C#C
C#2y ago
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;

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.
Was this page helpful?