© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•3y ago•
42 replies
Temptica

✅ Urgent | Cannot update property in EF because another instance with the same key is being tracked

Hi, so I have following code: Basically I get a batch to update the field
var batch = await context.Batch.FirstOrDefaultAsync(b => b.BatchId == id);
if (batch == null) throw new BatchNotFoundException($"No batch found with id {id}");
context.Entry(batch).Property(b => b.Status).IsModified = true;
batch.Status = status;
var batch = await context.Batch.FirstOrDefaultAsync(b => b.BatchId == id);
if (batch == null) throw new BatchNotFoundException($"No batch found with id {id}");
context.Entry(batch).Property(b => b.Status).IsModified = true;
batch.Status = status;

I get the error on the 'entry' line. If I leave it, it doesn't update the field (yes I do commit later on in my CQRS).

I have a code freeze tomorrow night so henc ethe Urgency as It's an imprtant feature the client wants which they only let us know very late...

Full error
The instance of entity type 'Batch' cannot be tracked because another instance with the same key value for {'BatchId'} 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.
The instance of entity type 'Batch' cannot be tracked because another instance with the same key value for {'BatchId'} 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# banner
C#Join
We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
61,871Members
Resources

Similar Threads

Was this page helpful?
Recent Announcements

Similar Threads

✅ EF: Instance of Entity Type cannot be tracked (already being tracked)
C#CC# / help
8mo ago
✅ WPF. Instance with the same key value for {'Id'} is already being tracked
C#CC# / help
2y ago
✅ EF Core 8: System.InvalidOperationException: 'The instance of entity type cannot be tracked..
C#CC# / help
10mo ago
The instance of entity type 'OptimizerInstanceEntity' cannot be tracked
C#CC# / help
2y ago