问题
I have run into my first dissapointment with Entity Framework 4. It turns out that SQL CE, when used with EF4, does not support autogenerated primary keys. I get a System.Data.UpdateException from OnjectContext.SaveChanges() with this message:
Server-generated keys and server-generated values are not supported by SQL Server Compact.
So, now I have to manually generate keys for my entities. Assuming I want to use auto-incremented integer keys, what is the best way to go about generating and keeping track of the keys when using Entity Framework? Thanks for your help.
回答1:
At the bottom of the link in your question is a suggested solution. See SQL Compact, Identity Columns and Entity Framework perhaps you might want to implement an extension method like the one outlined.
回答2:
I don't think David's idea is a good idea, however, I have no choice. Maybe CE will support autogenerated primary keys in next version.
来源:https://stackoverflow.com/questions/2480084/generating-entity-keys