I'm creating a rewrite of an existing application in EF Code First, but I need to import some of the data from the existing application's database. Here's the definition of my entity class:
public class Business : EntityBase
{
[Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int AccountNumber { get; set; }
Now, the imported records need to retain their AccountNumber
value from the old system. However, new values should use the IDENTITY
value generated by the DB.
How do I turn off the IDENTITY
while I'm importing old records, then turn it back on for the remainder of the lifetime of the application? Executing this before importing the records has no effect:
context.Database.ExecuteSqlCommand("set identity_insert Businesses on");