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)
The database is a SQL Server 2005.
I have a target DB structure similar to the following:
MYID - int, primary key, identity specification (yes)
MYData - varchar(50)
In my migration program, I import the DB structure into the edmx. I then manually turn off the StoreGeneratedPattern.
In my migration program, I turn off the identity column as follows(I have verified it does indeed turn it off):
using (newDB myDB = new newDB())
{
//turn off the identity column
myDB.ExecuteStoreCommand("SET IDENTITY_INSERT SR_Info ON");
}
After the above code, I have the following 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();
}
When it gets to the myDB.SaveChanges(), it generates an exception: "Cannot insert explicit value for identity column in table".
I know the code works fine when I manually goto the SQL Server table and turn off the Identity Specification off.
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.
Any ideas for getting this to work right?
I used the following steps:
At this point, when I add a new record with MYID set, the DB overwrites teh value of MYID with the Identity value.
So far, no matter where I've added "myDB.ExecuteStoreCommand("SET IDENTITY_INSERT SR_Info ON");", it behalves the same way.
I have tried adding a stored procedure and it also doesn't work.
I suspect that ultimately, the migration will not be fully automated.
To insert explicit values into a SQL Server IDENTITY column, you need to manually enable IDENTITY_INSERT before calling SaveChanges() . The following example demonstrates how to set an explicit value to an id property.
You cannot INSERT values manually into an IDENTITY column.
If the value inserted is larger than the current identity value for the table, SQL Server automatically uses the new inserted value as the current identity value. The setting of SET IDENTITY_INSERT is set at execute or run time and not at parse time.
IDENTITY_INSERT off in SQL Server Once you have turned the IDENTITY_INSERT option OFF, you cannot insert explicit values in the identity column of the table. Also, the value will be set automatically by increment in the identity column if you try to insert a new record.
When column is identity, inserting record from Entity Framework will raise the following error:
Cannot insert explicit value for identity column in table 'MY_ENTITY' when IDENTITY_INSERT is set to OFF. To avoid error set entity property StoreGeneratedPattern="Identity"
<EntityType Name="MY_ENTITY">
<Key>
<PropertyRef Name="UserID" />
</Key>
<Property Name="RecordID" Type="int" StoreGeneratedPattern="Identity" Nullable="false" />
</EntityType>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With