-
Notifications
You must be signed in to change notification settings - Fork 3.2k
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 '**Entity**' cannot be tracked because another instance with the same key value for {'Id'} is already being tracked. #23615
Comments
I seem to be getting this all over the place lately ... I've also noticed if I post an array of something to the api that then gets an entity from the db and makes changes to it more than once EF gets all confused about what's needed rough steps to produce the issue ...
I get this error ^ |
@robinNode Please attach a small, runnable project or post a small, runnable code listing that reproduces what you are seeing so that we can investigate. @TehWardy Please open a new issue and attach a small, runnable project or post a small, runnable code listing that reproduces what you are seeing so that we can investigate. |
@ajcvickers |
@robinNode you seem to be using AutoMapper to map an entity instance to some other type, then map back to the entity type, and then attempting to interact with change tracking using the newly-converted instance. This isn't supported, since there are now two entity instances representing the same thing (i.e. with the same key). If I remove the back-and-forth mapping, everything works fine. Possible solutions are:
/cc @ajcvickers this bears some resemblance to the immutable scenario, where the user wants to replace an already-tracked instance with another instance. |
In addition to #20124, maybe some sort of Replace method on the change tracker... Are we tracking something like this already? |
I have got this issue and I resolved it with context.ChangeTracker.Clear() with automapper |
Are you able to supply a sample snippet, I'm also mapping an incoming dto to a dbEntity before saving changes, however I'm doing all the logic within the service layer of the project and don't have direct access to the context but the repository does. |
@robinNode After playing around with your example code, you are indeed asking EF Core to track multiple instances of the same entity. A simple fix is to do your mapping later on the modified entity:
|
Interesting. I'm sure when it comes to how I use EF i'm probably like many, massively abusing it, but I've found that as a testament to how well written it is sometimes just taking an out of the box approach can improve things. I found that a lot of my problems went away when I instanced up a new context for literally every single row operation on the db. Pooling helped offset the perf hits but on occasion I would get an error from one context where I had somehow attached it to another. Correct me if i'm wrong but Random thought Bear with me on this ...
With context pooling and pooled factories this seems to be really fast, (like stupid fast) Another idea I had was to put a thread safety wrapper around a singleton context and just throw out all changes after every transaction with the wrapper but that felt ugly and would likely result in poorer perf since in order to scale i'm using a ton of instances at the moment. but if I could "queue" work to be done on an EF context I could probably make that fairly decent as an approach. Side quest |
I thought it best to put this in its own ticket ^ |
Exception details :
The instance of entity type 'Entity' cannot be tracked because another instance with the same key value for {'Id'} is already being tracked. When attaching existing entities, ensure that only one entity instance with a given key value is attached. Consider using 'DbContextOptionsBuilder.EnableSensitiveDataLogging' to see the conflicting key values.
Steps:
Stack traces
Provider and Version information
EF Core version: Microsoft.EntityFrameworkCore (3.1.7)
Database provider: Npgsql.EntityFrameworkCore.PostgreSQL (3.1.4)
Target framework: 3.1.7
Operating system: Microsoft Windows 10 Enterprise 64bit
IDE: Visual Studio 2019 16.3
The text was updated successfully, but these errors were encountered: