cannot be tracked because another instance with the same key .NET Origin: Application & API OSI layer 7 · Application Last updated 13 Oct 2026

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.

Exact message you may see cannot be tracked because another instance with the same key valueis already being tracked
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

  1. Load the entity and copy values onto it (SetValues)
  2. Use AsNoTracking for read queries
  3. Keep DbContext per request

Detailed fix by platform

C#

  1. csharp
    var existing = await db.Products.FindAsync(dto.Id);
    db.Entry(existing!).CurrentValues.SetValues(dto);
    await db.SaveChangesAsync();

How to diagnose

  1. Tracking — Was the entity loaded earlier?
  2. 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:

Where this error sits

Origin: Application & API. Errors that are often confused with or caused by this one (click to open):

A second operation was started on this context — EF Core: A second operation was started on this context instance before a previous operation completed (.NET)A second operation wa…Cannot access a disposed context instance — ObjectDisposedException: Cannot access a disposed context instance (.NET)Cannot access a dispo…DbUpdateException — DbUpdateException: An error occurred while saving the entity changes. See the inner exception for details. (.NET)DbUpdateExceptionJsonException object cycle — JsonException: A possible object cycle was detected (.NET)JsonException object …No database provider has been configured — InvalidOperationException: No database provider has been configured for this DbContext (.NET)No database provider …requires a primary key to be defined — EF Core: The entity type 'X' requires a primary key to be defined (.NET)requires a primary ke…The LINQ expression could not be translated — EF Core: The LINQ expression 'x' could not be translated. Either rewrite the query in a form that can be translated, or switch to client evaluation explicitly (.NET)The LINQ expression c…Unable to create a 'DbContext' — dotnet ef: Unable to create a 'DbContext' of type ''. The exception 'Unable to resolve service for type DbContextOptions' was thrown while attempting to create an instance (.NET)Unable to create a 'D…cannot be…

Explore the map of this error Full error map →

🧠 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.

💬 What worked for others

No tips yet — did something else fix this for you?

Share what worked for you

Tips are checked by an editor before they appear. Links are removed.