Entity Framework SET IDENTITY_INSERT

Is there a way to force an ID value for a new object in EF when we have a column with an auto-incrementing identifier, i.e. use SET IDENTITY_INSERT behavior through EF?

Our requirement is that our creation form always shows a new unique identifier for the object that we create in an empty form before it is filled or saved. The idea is that this identifier can be read to someone over the phone, and then the user can fill out and save the form after the call is completed. We could reserve the identifier by inserting an empty row into the database, but we have unique columns and FK; instead, I made a “next identifier” table, which we increment with security locks, and I test this against the top ID in the object table to be careful. The idea was then to force use this new identifier when we write the object, but I don’t seehow to do it ef.

Is this possible - this is what I missed? I don’t think the ID even does this before insertion, so I don’t think that manually calling SET IDENTITY_INSERT around SaveChanges will help.

Or do I need to do something? I see alternatives:

  • Change our column of identifiers so that it is not an identifier and does not perform manual control of all this: there is inheritance of the table identifier, so this is also difficult.
  • Separate the database identifier and the user-visible identifier in a separate column and write down your unique identifier there.
  • An empty string to reserve the identifier as described above; some null-value changes and changes to our data reading code may be required to ignore these records.

! EF4 ( EDMX , POCOs), SQL Server 2008 , .

+5
2

Guid . , concurrency .. Guid . . , . , Guid, , (a) SaveChanges()...

+1

,

  • EF

context.SaveChanges() . , . /, , / . singleton / , .

  1. :

, . /. , .

0

All Articles