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

前端 未结 6 2210
日久生厌
日久生厌 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: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;
        }
    }
    

提交回复
热议问题