Search code examples
c#asp.net-coreasp.net-web-apientity-framework-core

The instance of entity type 'User' cannot be tracked because another instance with the same key value for 'Id' is already being tracked


I know about questions has already been asked, but solutions didn't help me.

The instance of entity type 'User' 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.

I use Identity update method and it is invoke this exception.

await _userManager.UpdateAsync(user);

Also I tried to use Repository pattern update and it is updating right.

public async Task UpdateAsync(TEntity entity)
{
    _dbSet.Update(entity);
    _context.Entry(entity).State = EntityState.Modified;
}

By using attach it isn't updating connected to user class(by navigation property).

public async Task UpdateAsync(TEntity entity)
{
    _dbSet.Attach(entity);
    _context.Entry(entity).State = EntityState.Modified;
}

What am i doing wrong? I think a class of Identity should work correctly.

UPDATE : The solution was that I tried to get attached tables from the database with the main one at once(eg Categories to Users) and work with them(which is not the right approach). You should follow the principle of getting only what you need at the moment(if you need to update a category, you don't need to do it through a "connected object" in case you are working with it). That is, to receive from the database only what will be processed. Otherwise, it will lead to similar errors. Specifically, this one also consisted in the fact that my DTOs were not configured in the same way and stored instances of the same user there, which caused a conflict.

Not the best way to use DTOs:

class ExampleCategoryDTO
{
    public int Id { get; set; }
    public string Name { get; set; }
    
    public IList<User> Users { get; set; } --- wrong
}

Wrong way to work with data:

var tmp = context.Include(x => x.Categories).Users.ToList();

// Changing some "tmp" variable data in categories

context.Update(tmp);
context.SaveChanges();

Thanks to those who tried to help and sorry for the lack of information (I didn't really understand where the problem was coming from)


Solution

  • In my case the error occurred because multiple instances of the same entity were being tracked by the Entity Framework context, causing a conflict. The issue was related to how related data was being fetched and updated. The problem arose when trying to update related entities through a main entity, which led to multiple instances of the same entity being tracked (in the way described before in the question "UPDATE" section).

    To avoid similar problems use proper way to work with data:

    • Avoid fetching related entities unnecessarily: When updating a specific entity, get only the data you need. If you need to update an object, do not include related data unless absolutely necessary.
    • Simplify DTOs: Ensure that your DTOs aren't complex for "no reason" or include navigation properties that could lead to multiple instances of the same entity being tracked.
    • Update entities in a correct way: When updating, ensure that you're only attaching and updating the specific entity instances that need changes. Avoid re-attaching entities that are already being tracked by the context.