Tuesday, April 6, 2010

Entity Framework and Database Generated Keys

Still learning the ins and outs of the Entity Framework and ran into a snag.  I added a record to a table and saved without problem.  The second record threw an error and when I drilled into the inner exception I found a problem with the table's primary key constraint.  Looking at the table I noticed that the first added record had a GUID of  0.  Whoops!

What's going on?  After a little investigating I discovered that none of the Primary key fields were set up properly by Visual Studio when it created them from the database.  Since I am letting the database generate the GUID's these fields need to have the StoreGeneratedPattern property set to "Identity," but VS had set them all to "None."  Once I made this change, all was good.  Now excuse me while I make this change to every entity in my model...

No comments:

Post a Comment