The instance of entity type 'X' cannot be tracked because another instance with the same key value for {'Id'} is already being tracked
Two different objects with the same primary key were attached to one DbContext.
Seen on:
.NET
Meaning
Typical in update endpoints: the entity was loaded (tracked) earlier in the request, then a new instance from the request body with the same Id is attached via Update(). Use the tracked instance, or load with AsNoTracking.
Common causes
- Calling Update() with a DTO-mapped entity after loading the same Id
- Long-lived context accumulating tracked entities
- Graph containing the same entity twice
⚡ Quick fix
- Load the entity and copy values onto it (SetValues)
- Use AsNoTracking for read queries
- Keep DbContext per request
Detailed fix by platform
C#
- csharp
var existing = await db.Products.FindAsync(dto.Id); db.Entry(existing!).CurrentValues.SetValues(dto); await db.SaveChangesAsync();
How to diagnose
- Tracking — Was the entity loaded earlier?
- Lifetime — Context scope
🔧 Still not fixed?
Many errors look alike. If the steps above didn’t solve it, one of these is probably what you’re facing:
Similar errors
- A second operation was started on this context EF Core: A second operation was started on this context instance before a previous operation completed
- Cannot access a disposed context instance ObjectDisposedException: Cannot access a disposed context instance
- DbUpdateException DbUpdateException: An error occurred while saving the entity changes. See the inner exception for details.
Most viewed in .NET
- NullReferenceException System.NullReferenceException: Object reference not set to an instance of an object
- FileNotFoundException assembly Could not load file or assembly 'X, Version=…' or one of its dependencies
- InvalidOperationException InvalidOperationException: Unable to resolve service for type 'X' while attempting to activate 'Y'
Other ways to find it
🧠 Still stuck? Analyze your error
Paste the full message, response headers or stack trace — we'll detect the platform and point to the most likely cause.
Was this page helpful?
Report a correction or suggest an improvement
Last updated 7 Oct 2026