views:

49

answers:

1

For my primary key and identity in our Clients table I use uniqueidentifier column with a default value set to newsequentialid(). Inserting new rows through the management tool creates the values for the ID fine.

Inserting from .NET 4 code via EF results into zero GUIDs (00000-0000....) being inserted. I create the entity with new, set some values and perform EF AddToClients (for instance). If debugged, value for the id property shows zero GUID. I do not explicitly set the id via the Guid.NewGuid() in my code because I want to leave it off to the newsequentialid() in SQL Server. The problem is, it doesn't work. The first time the code is executed row gets inserted with zero GUID. The next time it obviously fails and results in primary key violation exception.

How can I get this to work without having to set the GUID in my client code?

+1  A: 

You need to change the StoreGeneratedPattern value for the property in the EF designer. Set it to Identity. This will cause EF to avoid setting the value on an insert, then capture it after the insert is complete.

Note that if you're using EF 4, you may have trouble with this using the designer only (see this link). You may have to edit the .edmx manually and set the StoreGeneratedPattern in the storage model itself.

Adam Robinson
Did it. No change. The entity still has ZERO GUID set prior to insertion and insertion fails with duplicate key (I left the previously inserted one in the table).
mare
ohh i see about the designer issues...this is just great..ON top of other issue I wrote about in another question, now this. Thanks for the answer though.
mare
@mare: I feel your pain. EF is a powerful tool, but when you encounter something that it doesn't handle *well*, it can be an enormous headache to get around it.
Adam Robinson
@adam: enormous is certainly the right word because this is a major pain in the ..., just reading this http://geeksharp.com/2010/05/27/ef4-bug-in-storegeneratedpattern-ssdl/
mare