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

前端 未结 6 2221
日久生厌
日久生厌 2021-02-14 13:21

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

[Fact]
public async Task UpdateAsync()
{
    string newTitle = \"newTitle1\";         


        
相关标签:
6条回答
  • 2021-02-14 13:40

    In my case I hit this error when running SaveChanges twice inside of two IFs statements. I moved the SaveChanges outside of those two blocks of code. Just a side note in my service layer it is querying the data with AsNoTracking();

    if (user.SendPaymentStatus)
    {
        user.SendPaymentStatus = false;
        saveChanges = true;
        //_userService.SaveChanges(user, false);
    
        msg = GetPaymentHTML(user.MasterNodeName, user.Payee, DbMasterNode.LastPaidUtc);
        Framework.Email.SendEmail(email, "MasterNode Payment - " + user.MasterNodeName, msg);        
    }
    
    if (user.SendNodeStatus)
    {
        user.SendNodeStatus = false;
        saveChanges = true;
        //_userService.SaveChanges(user, false);
    
        msg = GetStatusHTML(user.MasterNodeName, user.Payee, DbMasterNode.CurrentStatus, DbMasterNode.LastSeenUtc);
        Framework.Email.SendEmail(email, "MasterNode Down - " + user.MasterNodeName, msg);
    }
    
    if (saveChanges)
    {
        user.SendPaymentStatus = false;
        _userService.SaveChanges(user, false);
    }
    
    0 讨论(0)
  • 2021-02-14 13:40

    I was getting the same problem when was trying to update the value. then i found the proble i was using this.

    services.AddDbContext<StudentContext>(option => option.UseSqlServer(Configuration.GetConnectionString("databasename")),ServiceLifetime.Singleton);
    

    then i remove lifetime and it worked well for me.

    services.AddDbContext<StudentContext>(option => option.UseSqlServer(Configuration.GetConnectionString("databasename")));
    
    0 讨论(0)
  • 2021-02-14 13:44

    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 });
    
    0 讨论(0)
  • 2021-02-14 13:58

    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;
        }
    }
    
    0 讨论(0)
  • 2021-02-14 14:00

    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);
    
    0 讨论(0)
  • 2021-02-14 14:02

    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 = "john@mail.com" });
        storeContext .Stores.Add(new Store { Title = "Jennifer's store", Address = "Sydney", Description = "Fashion", SellerId = "jennifer@mail.com" });
        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 = "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 one of many reasons why dbContext should be limited by a scope. Thanks for the hint.

    0 讨论(0)
提交回复
热议问题