I'm using Entity Framework 6 as my ORM for talking to a SQL Server database. I have two scenarios when inserting new records:
The GUID of the record is empty, in which case I add the attribute
DatabaseGenerated(DatabaseGeneratedOption.Identity)]
above my ID fields on my POCOs, and the database happily generates a new sequential GUID for my record.
This scenario is best for indexing. My client first talks to an API which tries to insert a record into the database and have the database generate a sequential GUID.
The GUID of the record is already set, in which case that GUID should be set in the database. However, EF ignores this, and asks the database to generate a new sequential GUID anyway.
This scenario is in case the client is offline or otherwise can't reach the API. In this case, it will persist data locally with its own generated GUID, and then try and save that to the database later.
I've done some searching and can't seem to find any way to only conditionally ask the database to generate a new GUID if one isn't already set. Is this possible?
Thanks!