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.
```C# Codes
var testEntityModel = Mapper.Map
var testEntity = Mapper.Map
DbContext.TestEntity.RemoveRange(testEntity ); -- the exception throw out.
DbContext.SaveChanges();
### Stack traces
at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.IdentityMap1.ThrowIdentityConflict(InternalEntityEntry entry)
at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.IdentityMap1.Add(TKey key, InternalEntityEntry entry, Boolean updateDuplicate)
at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.IdentityMap1.Add(TKey key, InternalEntityEntry entry)
at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.IdentityMap1.Add(InternalEntityEntry entry)
at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.StateManager.StartTracking(InternalEntityEntry entry)
at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.InternalEntityEntry.SetEntityState(EntityState oldState, EntityState newState, Boolean acceptChanges, Boolean modifyProperties)
at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.InternalEntityEntry.SetEntityState(EntityState entityState, Boolean acceptChanges, Boolean modifyProperties, Nullable1 forceStateWhenUnknownKey)
at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.EntityGraphAttacher.PaintAction(EntityEntryGraphNode1 node)
at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.EntityEntryGraphIterator.TraverseGraphTState
at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.EntityGraphAttacher.AttachGraph(InternalEntityEntry rootEntry, EntityState targetState, EntityState storeGeneratedWithKeySetTargetState, Boolean forceStateWhenUnknownKey)
at Microsoft.EntityFrameworkCore.DbContext.SetEntityState(InternalEntityEntry entry, EntityState entityState)
at Microsoft.EntityFrameworkCore.DbContext.RemoveRange(IEnumerable1 entities)
at Microsoft.EntityFrameworkCore.DbContext.RemoveRange(Object[] entities)
at Microsoft.EntityFrameworkCore.Internal.InternalDbSet1.RemoveRange(TEntity[] entities)
at ...........
at Castle.Proxies.Invocations.ISolutionSharedRepository_LoadSolutions.InvokeMethodOnTarget()
at Castle.DynamicProxy.AbstractInvocation.Proceed()
at ...........
```
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
I seem to be getting this all over the place lately ...
Did something change recently to cause this to happen more often?
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.
@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
I have attached a demo. please take a look at it. thanks a lot.
EFCoreTest.zip
@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:
context.Entry(original).State = EntityState.Detached;). Because EF is no longer tracking the original instance, you can now pass it your new, mapped one. However, this is not a recommended technique; while it will work for deleting, for updating it means EF can no longer automatically determine which properties change, so you would have to specify that yourself./cc @ajcvickers this bears some resemblance to the immutable scenario, where the user wants to replace an already-tracked instance with another instance.
@roji Yes, I have been thinking a lot about the immutable scenario, and I think both this and that are dependent on #20124. It's then about conventions and patterns for how resolution happens in that hook. I agree that we should think about these things in a holistic way.
In addition to #20124, maybe some sort of Replace method on the change tracker... Are we tracking something like this already?
Most helpful comment
@roji Yes, I have been thinking a lot about the immutable scenario, and I think both this and that are dependent on #20124. It's then about conventions and patterns for how resolution happens in that hook. I agree that we should think about these things in a holistic way.