title: IDENTITY_INSERT during seeding with EntityFramework 6 Code-First
body: <p>I have an entity that has an <code>Auto-identity (int)</code> 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.</p>
  
  <p>So far I've been able to set the <code>IDENTITY_INSERT</code> to On as part of the insert batch, but Entity Framework does not generate an insert statement that include the <code>Id</code>. This makes sense as the model thinks the database should provide the value, but in this case I want to provide the value.</p>
  
  <p>Model (pseudo code):</p>
  
  <pre><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&lt;ReferenceThing&gt;.Add(myThing);
  
          context.Database.Connection.Open();
          context.Database.ExecuteSqlCommand("SET IDENTITY_INSERT ReferenceThing ON")
  
          context.SaveChanges();  // &lt;-- generates SQL INSERT statement
                                  //     but without Id column value
  
          context.Database.ExecuteSqlCommand("SET IDENTITY_INSERT ReferenceThing OFF")
      }
  }
  </code></pre>
  
  <p>Anyone able to offer any insight or suggestions?</p>