Updating existing data in EF 6 throws exception - “…entity of the same type already has the same primary key value.”

前端 未结 4 1739
眼角桃花
眼角桃花 2020-12-29 13:39

I am trying to update a record using Entity Framework 6, code-first, no fluent mapping or a tool like Automapper.

The entity(Employee) has other composi

4条回答
  •  一个人的身影
    2020-12-29 14:18

    You need to detach to avoid duplicate primary key exception whist invoking SaveChanges

    db.Entry(entity).State = EntityState.Detached;
    

提交回复
热议问题