Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
455 views
in Technique[技术] by (71.8m points)

entity framework core - How can I avoid Tracking error when deleting the same ID twice?

I implemented a simple Delete method in my ServiceStack Service class as follows...

        public void Delete(DeleteCompany d)
        {
           
            Company c = new Company();
            c = ctx.Companies.AsNoTracking().FirstOrDefault(m => m.id == d.id);
            if (c == null)
            {
                return;
            }

            ctx.Companies.Remove(c);                
            ctx.SaveChanges();
                      
        }

ctx is populated via Dependency Injection.

This works great the first time I delete an object. But if I create another object with the same ID and then attempt to delete it, I get this exception...

System.InvalidOperationException HResult=0x80131509 Message=The instance of entity type 'Company' 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. Source=Microsoft.EntityFrameworkCore
StackTrace: 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.NullableKeyIdentityMap1.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.TraverseGraph[TState](EntityEntryGraphNode1 node, Func2 handleNode) at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.EntityGraphAttacher.AttachGraph(InternalEntityEntry rootEntry, EntityState targetState, EntityState storeGeneratedWithKeySetTargetState, Boolean forceStateWhenUnknownKey) at Microsoft.EntityFrameworkCore.Internal.InternalDbSet1.SetEntityState(InternalEntityEntry entry, EntityState entityState) at Microsoft.EntityFrameworkCore.Internal.InternalDbSet1.Remove(TEntity entity) at WebApp.ServiceInterface.CompanyService.Delete(DeleteCompany d) in C:ProjectsNCMSAuditServiceStackNCMSProgramNCMSProgram.ServiceInterfaceCompanyService.cs:line 81 at ServiceStack.Host.ServiceExec1.<>c__DisplayClass3_0.<CreateExecFn>b__0(Object service, Object request) in C:BuildAgentwork3481147c480f4a2fsrcServiceStackHostServiceExec.cs:line 177 at ServiceStack.Host.ServiceRunner1.d__15.MoveNext() in C:BuildAgentwork3481147c480f4a2fsrcServiceStackHostServiceRunner.cs:line 134

How can I improve my code so I don't get this exception?

question from:https://stackoverflow.com/questions/65617375/how-can-i-avoid-tracking-error-when-deleting-the-same-id-twice

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...