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

45,035

Solution 1

Had the same problem with EF core 2.2. I never experianced this with other applications.

Ended up rewriting all my update functions somehow like this:

public bool Update(Entity entity)
{
    try
    {   
       var entry = _context.Entries.First(e=>e.Id == entity.Id);
       _context.Entry(entry).CurrentValues.SetValues(entity);
       _context.SaveChanges();
       return true;
    }
    catch (Exception e)
    {
         // handle correct exception
         // log error
         return false;
    }
}

Solution 2

Numerous issues I've been running into have one nasty root. In a nutshell: I've learned the hard way why dbContext is scoped rather than singleton. Here is Store type, but the issue was the same. Here is simplified test initialization code

public TestBase()
{
    services = new ServiceCollection();
    storeContext = StoreContextMock.ConfigureStoreContext(services, output);
    serviceProvider = services.BuildServiceProvider();
}
public static StoreContext ConfigureStoreContext(IServiceCollection services)
{
    services.AddDbContext<StoreContext>(c =>
        c.UseInMemoryDatabase(Guid.NewGuid().ToString()).UseQueryTrackingBehavior(QueryTrackingBehavior.NoTracking));

    var serviceProvider = services.BuildServiceProvider();
    var storeContext = serviceProvider.GetRequiredService<StoreContext>();
    storeContext .Stores.Add(new Store { Title = "John's store", Address = "NY", Description = "Electronics best deals", SellerId = "[email protected]" });
    storeContext .Stores.Add(new Store { Title = "Jennifer's store", Address = "Sydney", Description = "Fashion", SellerId = "[email protected]" });
    storeContext .SaveChanges();
    return storeContext ;
}

I reread error and finally noticed the main word

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

So there has to be some orphan tracked instance preventing me from working with store. I did not save any references to s1 or s2, so it must be storeContext storing references on inserted objects even after leaving scope of their declaration and initialization. That's why I was unable update variables normally and also why my 'queried' from db objects had all their navigation properties assigned (lazy loading has little to do with this). The following code resolved all my issues.

public static StoreContext ConfigureStoreContext(IServiceCollection services)
{
    services.AddDbContext<StoreContext>(c =>
        c.UseInMemoryDatabase(Guid.NewGuid().ToString()).UseQueryTrackingBehavior(QueryTrackingBehavior.NoTracking));

    var serviceProvider = services.BuildServiceProvider();
    var storeContext = serviceProvider.GetRequiredService<StoreContext>();
    var s1 = new Store { Title = "John's store", Address = "NY", Description = "Electronics best deals", SellerId = "[email protected]" };
    var s2 = new Store { Title = "Jennifer's store", Address = "Sydney", Description = "Fashion", SellerId = "[email protected]" }
    storeContext .Stores.Add(s1);
    storeContext .Stores.Add(s2);
    storeContext .Entry<Store>(s1).State = EntityState.Detached;
    storeContext .Entry<Store>(s2).State = EntityState.Detached;
    storeContext .SaveChanges();
    return storeContext ;
}

That is one of many reasons why dbContext should be limited by a scope. Thanks for the hint.

Solution 3

Alexandar's answer, which was to disable tracking completely, solved my issue, but I got worried since I didn't know what this would do to the rest of my application. So I went to the Microsoft docs and found this:

You should not disable change tracking if you want to manipulate entity instances and persist those changes to the database using SaveChanges().

This method sets the default behavior for all contexts created with these options, but you can override this behavior for a context instance using QueryTrackingBehavior or on individual queries using the AsNoTracking(IQueryable) and AsTracking(IQueryable) methods.

So the solution for me was to disable tracking only when needed. So I solved my issue by using this in the other part of my code that retrieved the same entry from the database:

var entry = await context
    .SomeDbTable
    .AsNoTracking() // this is what you're looking for
    .Find(id);

Solution 4

For me was this the solution:

public void Update(int id, T obj)
        {
            var entry = table.Find(id);
            _context.Entry(entry).CurrentValues.SetValues(obj);
        }

Based on the solution Bryan gave. I think I use newer version of EF/Automapping. This works for me.

Solution 5

I had same problem while I was copying some records in database by Entity Framework and changing one column that was other's entity key. Tracking mode change did not fix the issue.

The issue was fixed by properly setting primary key in EntityTypeConfiguration, to contain the changed value here described as x.EntityTwoKey.

builder.HasKey(x => new { x.EntityOneKey, x.EntityTwoKey });
Share:
45,035
Alexander Kozachenko
Author by

Alexander Kozachenko

Updated on February 16, 2022

Comments

  • Alexander Kozachenko
    Alexander Kozachenko about 2 years

    I am aware that such question has already been asked, but solution did not help me.

    [Fact]
    public async Task UpdateAsync()
    {
        string newTitle = "newTitle1";
        int newBrandId = 3;
        var item = await storeContext.Items.AsNoTracking().FirstOrDefaultAsync();
        item.BrandId = newBrandId;
        item.Title = newTitle;
        storeContext.Entry(item).State = EntityState.Detached;
        await service.UpdateAsync(item); // exception inside
        var updatedItem = await storeContext.Items.AsNoTracking().FirstOrDefaultAsync();
        Assert.Equal(newTitle, updatedItem.Title);
        Assert.Equal(newBrandId, updatedItem.BrandId);
    }
    
    public async Task UpdateAsync(T entity)
    {
        _dbContext.Entry(entity).State = EntityState.Modified; // exception when trying to change the state
        await _dbContext.SaveChangesAsync();
    }
    

    Message: System.InvalidOperationException : The instance of entity type 'Item' 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. Consider using 'DbContextOptionsBuilder.EnableSensitiveDataLogging' to see the conflicting key values.

    interesting that exception is the same even if no item retreived from db, like so

    //var item = await storeContext.Items.AsNoTracking().FirstOrDefaultAsync();
      var item = new Item()
      {
          Id = 1,
          BrandId = newBrandId,
          CategoryId = 1,
          MeasurementUnitId = 1,
          StoreId = 1,
          Title = newTitle
      };