Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

The instance of entity type 'XXXX' cannot be tracked because another instance with the key value 'XXX' is already being tracked. When attaching existing entities, ensure that only one entity instance with a given key value is attached. #33629

Closed
luisf350 opened this issue Apr 28, 2024 · 7 comments
Labels
closed-no-further-action The issue is closed and no further action is planned. customer-reported

Comments

@luisf350
Copy link

I'm getting that particular issue trying to update an entity.
I try to add .UseQueryTrackingBehavior(QueryTrackingBehavior.NoTracking) in DataContext but didn't work.
Also do same thing in Repository class. My can update the record the 1st time, but, if I want to perform anothe modification for same entity I got that error.

In other to analyze the issue, I create a WPF solution with same issue but, in this case, Im having the problem the 1st time trying to update:
image
EFCore.zip

@archer87pl
Copy link

Do you have optional relationship defined? If yes then please take a look at #33610

@luisf350
Copy link
Author

Hi @archer87pl
No. As you can see in the solution that I attach, there is only 1 entity defined so, no relationships on it

@ajcvickers
Copy link
Contributor

@luisf350 The code creates a single instance of your DbContext type in MainWindowViewModel and then uses this for evermore. Instead, create a new context instance for each unit-of-work.

@luisf350
Copy link
Author

Hi @ajcvickers
In my code, I don't have any implementation of DbContext directly on MainWindowViewModel. I'm using the Domain, which also uses the Repository that uses the DbContext.
Now, I have changed the registration type for DbContext. Instead of having it as Singleton, I tried with Scoped and Transient to get the same result.
Were you able to perform an update with the given solution?

@ajcvickers
Copy link
Contributor

@luisf350 The way you are using D.I. means that, as far as I can tell, you will never get more than one DbContext instance, because it is only (indirectly) requested from the service provider once.

@luisf350
Copy link
Author

luisf350 commented May 1, 2024

Hi @ajcvickers
Do you have any suggestion in the given solution to use Context with Dependency injection and avoid the issue I'm facing?

@ajcvickers
Copy link
Contributor

@luisf350 It's hard to say because you know what your intention is. All I would say is that, if you are going to do a repository, with or without D.I., then make sure it is set up so that it does not re-use the same context instance for multiple unit-of-work. Also, don't just call Update to mark everything as modified--this is rarely the best pattern. Consider reading the docs on change tracking for more information.

@ajcvickers ajcvickers closed this as not planned Won't fix, can't repro, duplicate, stale May 7, 2024
@ajcvickers ajcvickers added closed-no-further-action The issue is closed and no further action is planned. and removed area-change-tracking labels May 7, 2024
@ajcvickers ajcvickers removed their assignment May 7, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
closed-no-further-action The issue is closed and no further action is planned. customer-reported
Projects
None yet
Development

No branches or pull requests

3 participants