Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

IDENTITY_INSERT during seeding with EntityFramework 6 Code-First

Tags:

I have an entity that has an Auto-identity (int) column. As part of the data-seed I want to use specific identifier values for the "standard data" in my system, after that I want to have the database to sort out the id value.

So far I've been able to set the IDENTITY_INSERT to On as part of the insert batch, but Entity Framework does not generate an insert statement that include the Id. This makes sense as the model thinks the database should provide the value, but in this case I want to provide the value.

Model (pseudo code):

public class ReferenceThing {     [Key]     [DatabaseGenerated(DatabaseGeneratedOption.Identity)]     public int Id{get;set;}     public string Name{get;set;} }  public class Seeder {     public void Seed (DbContext context)     {          var myThing = new ReferenceThing         {             Id = 1,             Name = "Thing with Id 1"         };          context.Set<ReferenceThing>.Add(myThing);          context.Database.Connection.Open();         context.Database.ExecuteSqlCommand("SET IDENTITY_INSERT ReferenceThing ON")          context.SaveChanges();  // <-- generates SQL INSERT statement                                 //     but without Id column value          context.Database.ExecuteSqlCommand("SET IDENTITY_INSERT ReferenceThing OFF")     } } 

Anyone able to offer any insight or suggestions?

like image 676
RikRak Avatar asked Jun 17 '14 13:06

RikRak


People also ask

What does Identity_insert mean?

IDENTITY_INSERT is a table property that allows you to insert explicit values into the column of table identifiers, i.e. into the column with IDENTITY. The value of the inserted identifier can be either less than the current value or more, for example, to skip a certain interval of values.

How Identity_insert is set to off?

There are two ways: Either you turn off the Identity property by going into design, and opting for column properties, i.e., using GUI. After inserting that value, again making it ON, so that it will continue incrementing it. Or using T-SQL code, which I will explain you below-

How do you check Identity_insert is on or off for a table?

Answers. In a given session , you can have only one table's IDENTITY_INSERT property set to ON. You can use set IDENTITY_INSERT state (on/off) only at excute or run time.


1 Answers

So I might have resolved this one by resorting to generating my own SQL insert statements that include the Id column. It feels like a terrible hack, but it works :-/

public class Seeder {     public void Seed (DbContext context)     {          var myThing = new ReferenceThing         {             Id = 1,             Name = "Thing with Id 1"         };          context.Set<ReferenceThing>.Add(myThing);          context.Database.Connection.Open();         context.Database.ExecuteSqlCommand("SET IDENTITY_INSERT ReferenceThing ON")          // manually generate SQL & execute         context.Database.ExecuteSqlCommand("INSERT ReferenceThing (Id, Name) " +                                            "VALUES (@0, @1)",                                             myThing.Id, myThing.Name);          context.Database.ExecuteSqlCommand("SET IDENTITY_INSERT ReferenceThing OFF")     } } 
like image 76
RikRak Avatar answered Nov 08 '22 11:11

RikRak