ObjectContext.SaveChanges() violates primary key, throws UpdateException?

后端 未结 2 1756
深忆病人
深忆病人 2020-12-20 02:17

Paraphrasing from this MSDN documentation ...

An INSERT statement is generated by the Entity Framework and executed on the data source when SaveCh

2条回答
  •  [愿得一人]
    2020-12-20 02:33

    This problem was caused by a bug in EF4 where the EF designer doesn't set the StoreGeneratedPattern attribute in the SSDL. The problem is documented in this blog and this Microsoft Connect ticket.

    The solution was to open my model's .edml file in a text editor, locate the XML tag, and add StoreGeneratedPattern="Identity" to the property tag being used as an EntityKey:

      
         
         
         ...
      
    

    I then re-opened my model in the EF designer, made a small change (moved an entity position) and saved. This caused the code generator to run, presumably synchronizing the CDSL with the SSDL.

    My code now runs as expected.

提交回复
热议问题