Hi, I am doing a little test with EF4, and I've tried to set up one entity which the primary key is one Guid,
Till this point, every thing is ok!, but whene try to insert into database, the visual studio throw one error saying that the object can't be saved because the Id (that´s my primary key) is null, otherwise the EF don't genarate one Guid for Id!
What can I do to solve my problem?
- Moved by VMazurModerator Thursday, April 29, 2010 10:37 AM (From:ADO.NET Managed Providers)
Is the GUID primary key column auto-generated at the database side? Please set the StoreGeneratedPatterns to Identity on the GUID PK property. For detail, please see http://blogs.msdn.com/alexj/archive/2009/05/20/tip-19-how-to-use-optimistic-concurrency-in-the-entity-framework.aspx & http://msdn.microsoft.com/en-us/library/dd283139.aspx.
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
- Proposed as answer by Michael Sun [MSFT]Microsoft employee, Moderator Wednesday, April 28, 2010 2:50 PM
Apparently using server side GUID as a primary key is not supported:
I am currently working on a project where this is required and I also use Entity Framework 4 and ran in a snag almost immediately. Entity Framework doesn't support server generated Guids when inserting entities because not all providers support it (http://msdn.microsoft.com/en-us/library/dd283139.aspx). This can pose a problem since you have to get the Guid generated for you somehow. When you insert an entity and the Guid is null, the call will fail because the primary key cannot be null.
- Proposed as answer by Scott Macleman Tuesday, May 04, 2010 2:17 PM
Please check this thread for a solution. There is a bug in the designer.
When you insert create a Guid for the pk. View the example below.
Guid hpHdrId = Guid.NewGuid();
handPunchHdr hphdr = new handPunchHdr();
hphdr.createdDate = DateTime.Now;
hphdr.hanchPunchHdrid = hpHdrId;
hphdr.filename = fi.FullName;