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

c# entity-framework-core

Popular Answer

Numerous problems There is one awful root that I keep running into. In a nutshell: dbContext is scoped rather than singleton, as I've discovered the hard way. Although this is a store type, the problem was the same. Here is a code snippet for initializing tests.

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 = "john@mail.com" });
    storeContext .Stores.Add(new Store { Title = "Jennifer's store", Address = "Sydney", Description = "Fashion", SellerId = "jennifer@mail.com" });
    storeContext .SaveChanges();
    return storeContext ;
}

Rereading the mistake, I at last discerned the key phrase.

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

Therefore, something orphaned monitored must be stopping me from using the shop. I didn't save any links tos1 or s2 thus, it must bestoreContext preserving references to inserted objects even after they have been initialized and declared outside of scope. Because of this, I was unable to change variables as usual, and the objects I "queried" from the database already had all of their navigation attributes set (lazy loading has little to do with this). All of my problems were fixed with the code below.

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 = "john@mail.com" };
    var s2 = new Store { Title = "Jennifer's store", Address = "Sydney", Description = "Fashion", SellerId = "jennifer@mail.com" }
    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 only one of many justifications for why a scope should be placed on dbContext. I appreciate the hint.

6
6/25/2018 3:34:16 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow