title: Entity Framework: Issue with IDENTITY_INSERT - "Cannot insert explicit value for identity column in table"
body: <p>I am using Entity Framework 4.0 with C#.NET.  I am trying to create a "simple" migration tool to convert data from one table to another(the tables are NOT the same)</p>
  
  <p>The database is a SQL Server 2005.</p>
  
  <p>I have a target DB structure similar to the following:</p>
  
  <pre><code>MYID - int, primary key, identity specification (yes)
  MYData - varchar(50)
  </code></pre>
  
  <p>In my migration program, I import the DB structure into the edmx.  I then manually turn off the StoreGeneratedPattern.</p>
  
  <p>In my migration program, I turn off the identity column as follows(I have verified it does indeed turn it off):</p>
  
  <pre><code>            using (newDB myDB = new newDB())
              {
                  //turn off the identity column 
                  myDB.ExecuteStoreCommand("SET IDENTITY_INSERT SR_Info ON");
              }
  </code></pre>
  
  <p>After the above code, I have the following code:</p>
  
  <pre><code>            using (newDB myDB = new newDB())
              {
                  DB_Record myNewRecord = new DB_Record();
                  //{do a bunch of processing}
                  myNewRecord.MYID = 50;
                  myDB.AddToNewTable(myNewRecord);
                  myDB.SaveChanges();
              }
  </code></pre>
  
  <p>When it gets to the myDB.SaveChanges(), it generates an exception: "Cannot insert explicit value for identity column in table".</p>
  
  <p>I know the code works fine when I manually goto the SQL Server table and turn off the Identity Specification off.</p>
  
  <p>My preference is to have the migration tool handle turning the Identity Specification on and off rather than have to manually do it on the database.  This migration tool will be used on a sandbox database, a dev database, a QA database, and then a production database so fully automated would be nice.</p>
  
  <p>Any ideas for getting this to work right?</p>
  
  <p>I used the following steps:</p>
  
  <ol>
  <li>Create database table with identity column.</li>
  <li>In Visual Studio, add a new EDMX.</li>
  <li>On EDMX, I select Update Model from Database (and select add the desired table)</li>
  <li>On EDMX, I click MYID and in the properties pane, I set StoreGeneratedProcedure to None.</li>
  </ol>
  
  <p>At this point, when I add a new record with MYID set, the DB overwrites teh value of MYID with the Identity value.</p>
  
  <p>So far, no matter where I've added "myDB.ExecuteStoreCommand("SET IDENTITY_INSERT SR_Info ON");", it behalves the same way.  </p>
  
  <p>I have tried adding a stored procedure and it also doesn't work.</p>
  
  <p>I suspect that ultimately, the migration will not be fully automated.</p>