How to update primary key from Entity Framework?

后端 未结 1 955
青春惊慌失措
青春惊慌失措 2021-01-02 03:17

I have Table

eventid int -- not PK key but with autoincrement
jobid -- PK autoincrement disabled
userid  int   -- PK autoincrement disabled

相关标签:
1条回答
  • 2021-01-02 03:29

    Updating primary key columns is not a good practice with EntityFramework. It confuses EF because it changes the identity of the object, and makes keeping the in-memory copy and the in-database copy of the data in sync very problematic. So it's not allowed.

    Just don't update primary keys. Instead delete one row and insert a new one.

    Alternatively you can update the primary key directly with a stored procedure or other query.

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