Attaching an entity of type 'X' failed because another entity of the same type already has the same primary key value

后端 未结 5 809
没有蜡笔的小新
没有蜡笔的小新 2021-01-04 09:25

ErrorMessage :

Attaching an entity of type \'FaridCRMData.Models.Customer\' failed because another entity of the same type already has the same prim

5条回答
  •  被撕碎了的回忆
    2021-01-04 10:06

    You need to detach the local version first and then do the update process

    var localEntity = dbContext.Set()
        .Local
        .FirstOrDefault(f => f.Id == theModel.Id);
    if (localEntity != null)
    {
        dbContext.Entry(localEntity).State = EntityState.Detached;
    }
    dbContext.Entry(appModel).State = EntityState.Modified;
    

提交回复
热议问题