I'm trying to setup the Entity Framework with SQL Server 2008. I'm using Guids for the keys on my tables. Is there a way to set it up so the keys are automatically generated by the database? I tried setting "RowGuid" to true and also set the column's default value to be "(newid())". Either way the mapped class still needs me to give it a Guid on the C# side. Any ideas?
Since a Guid is a structure, a null value is never passed so the defaults don't kick in. You could consider a nullable Guid: Guid?, though this may cause headaches as mentioned in this post:
http://stackoverflow.com/questions/211436/nullable-guid
I found it easier to just initialize the Guid with a new value:
private Guid _identifier = Guid.NewGuid();
If it's an existing record, it will be replaced when the object is populated. If not, the initialized value will be inserted.
17.4.Can I use a server-generated guid as my entity key?
Unfortunately, in v1 of the EF this is not supported. While it is possible with SQL Server to have a column of type “uniqueidentifier” and to set it’s default value to be “newid()”, with SQL Server 2000 there’s no good way to extract the value that the server generated at the time you do the insert. This can be done on more recent versions of SQL Server, though, so the intent is that we should find a clean way to special case this support in future versions of the EF so it can be supported on those databases that allow it and not on others. For now, the work around is to generate the guid on the client (ideally in the constructor of your object) rather than on the server.
Personally, I think that anyone who is using EF would be willing to give up compatibility with SQL Server 2000 to get the features they need. It seems like every halfway non-trivial thing that I need to do in EF is unsupported due to the fact that they have to keep compatibility with SQL Server 2000.
When will it ever end!