My application supports SQL Server and Compact edition. I use EntityFramework as the data access layer. To allow the users to go from Compact to SQL Server and back to Compact, I have a method to copy the data from one database to the other. This works fine from Compact to SQL Server, but when I use the same code to copy from SQL Server to Compact I get exceptions telling me that I cannot add a row with duplicate Id after an object was added to the EF data context and calling context.SaveChanges()
.
Here is how I copy the tables:
- I let EF create the destination database
- Open a SqlConnection(SqlCeConnection to source DB and destination DB
- Foreach table I:
- On the destination database SET IDENTITY_INSERT [Tablename] ON
- Copy the table row by row using Sql inserts, including the ID's
- On the destination database SET IDENTITY_INSERT [Tablename] OFF
So after I copy the database from SQL Server to Compact and using it with Entity Framework I get the above mentioned exception. So it looks like SET IDENTITY_INSERT ON
works (because the data gets into the Compact database) but SET IDENTITY_INSERT OFF
does not work on SQL Compact.
Does anyone has similar experiences and a solution for me?