locked
Entity Framework 6 and SQL Server merge replication RRS feed

  • Question

  • Hi all

    I've some trouble with Entity Framework 6 and SQL Server 2008R2 Merge Replication service.

    When I try to save an entity I get these messages: "Cannot insert duplicate key row in object 'dbo.xxx' with unique index 'MSmerge_index_xxx'. The duplicate key value is (00000000-0000-0000-0000-000000000000)."

    I'm using the merge replication service on my database and this has added, in each table, the column rowguid with default value "newsequentialid()"

    EF6 supports guid generated from the server?

    How can i solve this trouble without generrating the Guid from code side? This would force me to change all the queries of my project.

    Monday, May 26, 2014 8:04 AM

Answers

  • Hello Daniele,

    If you are using database first approach, then you can have a try to modify the .edmx file opened with XML editor. For example, in your case with a NEWSEQUENTIALID() filed:

    <Property Name="Table27ID" Type="uniqueidentifier" Nullable="false" />

    To

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

    It would not mark the key property as StoreGeneratedPattern="Identity" by default, we need to add it by hand, maybe it will be changed in the further released.

    It would generate sequential guid id:

    If you are using code first approach, then you can have a look this link:

    http://stackoverflow.com/questions/13012099/entity-framework-5-code-first-newsequentialid-as-pk

    Using migrations to simply modify code based migration to use newsequentialid() as DefaultValueSql

    Regards.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.


    • Edited by Fred Bao Tuesday, May 27, 2014 3:34 AM
    • Marked as answer by Daniele Mereu Wednesday, May 28, 2014 10:08 AM
    Tuesday, May 27, 2014 3:33 AM

All replies

  • Hello Daniele,

    If you are using database first approach, then you can have a try to modify the .edmx file opened with XML editor. For example, in your case with a NEWSEQUENTIALID() filed:

    <Property Name="Table27ID" Type="uniqueidentifier" Nullable="false" />

    To

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

    It would not mark the key property as StoreGeneratedPattern="Identity" by default, we need to add it by hand, maybe it will be changed in the further released.

    It would generate sequential guid id:

    If you are using code first approach, then you can have a look this link:

    http://stackoverflow.com/questions/13012099/entity-framework-5-code-first-newsequentialid-as-pk

    Using migrations to simply modify code based migration to use newsequentialid() as DefaultValueSql

    Regards.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.


    • Edited by Fred Bao Tuesday, May 27, 2014 3:34 AM
    • Marked as answer by Daniele Mereu Wednesday, May 28, 2014 10:08 AM
    Tuesday, May 27, 2014 3:33 AM
  • Hi Fred

    Thanks for the reply.

    Yes, I'm using database first approach then the solution that you prosed works fine.

    Another possible solution is to create the Entity Framework file before creating the subscription in order to create the rowguid field directly from the sql server.

    Regards

    Wednesday, May 28, 2014 10:08 AM