I have EF 6.x, several entities and with an Id field for each (like Guid), which are configured as

Property(e => e.Id).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity); 

I receive data when synchronizing with the server and I need to keep models with server identifiers in my database.
But, unfortunately, the only method that I found is to create a new context, but this method is extremely undesirable for me.
I also found options for offering to put DatabaseGeneratedOption.None and set the default value for the field, but it also doesn’t work, since The primary key cannot be null.

PS SET IDENTITY_INSERT also not suitable.

Thanks for any replies!

    1 answer 1

    You are trying to solve two problems in different ways at the same time - therefore it does not work. On the one hand, you need to generate Id in the database when you create a new record (because DatabaseGeneratedOption.Identity ) - on the other hand, you need so that you can set them yourself (because DatabaseGeneratedOption.None ).

    So - Entity Framework does not know how .


    The variant with Identity excludes the insertion of its values ​​in principle. EF does not even try to transfer them to the database (and therefore IDENTITY_INSERT does not work).

    Therefore, you need an option with None . And so that the database does not complain that "primary key cannot be null" - do not forget to fill this field! When you make a primary key with the DatabaseGeneratedOption.None attribute, you tell EF that you will always specify its value. So create it with Guid.NewGuid() .