none
SaveChanges with GUID as EntityKey RRS feed

  • Question

  • I have a SQL Server 2008 database table that uses uniqueidentifier as a primary key. On inserts, the key is generated on the database side using the newid() function.

    This works fine with ADO.NET. But when I set up this table as an entity in an Entity Framework 4 model, there's a problem. I am able to query the entity just fine, but when creating a new entity and invoking SaveChanges() on the context, the generated uniqueidentifier on the database is all zeros.

    I understand there was an issue with EF v1 where this scenario did not work, requiring creating the GUID on the client prior to calling SaveChanges. However, I had read in many places that they were planning to fix this in EF 4.

    My question -- is this scenario (DB-side generation of uniqueidentifier) still not supported in EF4? Are we still stuck with generating the GUID on the client?

    Thursday, May 13, 2010 1:39 PM

Answers

  • When you mark a property as StoreGenerated="Identity" in the designer, it adds an annotation to the property in the ConceptualModel in the edmx but does not add the required attribute on the property in the StorageModel. Please open the edmx file in xml editor and add the following attribute to the property in the EntityType in the edmx:StorageModels:

    <

     

     

    Property Name="GuidId" Type="uniqueidentifier" Nullable="false" StoreGeneratedPattern="Identity"/>

    Thanks

    Srikanth


    This posting is provided "AS IS" with no warranties, and confers no rights.
    • Marked as answer by LanceLinq Friday, May 14, 2010 12:17 AM
    Thursday, May 13, 2010 9:12 PM

All replies

  • Is the ID marked as Store Generated: Identity in the model?  You should be able to set this on the ID in the designer or the EDMX/CSDL and then it should work.

    Hope this helps,

    Arthur

     

    Thursday, May 13, 2010 4:51 PM
    Moderator
  • Yes, I changed StoreGenerated to Identity (and tried Computed as well) and neither worked. It's like it is trying to insert a "null" value for the GUID, but of course since GUID is a struct I'm just getting all zeros.
    Thursday, May 13, 2010 8:41 PM
  • When you mark a property as StoreGenerated="Identity" in the designer, it adds an annotation to the property in the ConceptualModel in the edmx but does not add the required attribute on the property in the StorageModel. Please open the edmx file in xml editor and add the following attribute to the property in the EntityType in the edmx:StorageModels:

    <

     

     

    Property Name="GuidId" Type="uniqueidentifier" Nullable="false" StoreGeneratedPattern="Identity"/>

    Thanks

    Srikanth


    This posting is provided "AS IS" with no warranties, and confers no rights.
    • Marked as answer by LanceLinq Friday, May 14, 2010 12:17 AM
    Thursday, May 13, 2010 9:12 PM
  • Thanks Mr. Mandadi, that is exactly the right answer. Works a charm!
    Friday, May 14, 2010 12:18 AM