I have to be able to set the primary key of the entities I am trying to add in my list \"final_zones\". I have a controller constructor as follows:
publi
I solved it with the following:
using (var dbContextTransaction = _context.Database.BeginTransaction())
{
try
{
_context.Database.ExecuteSqlCommand(@"SET IDENTITY_INSERT [CFETSWeb].[dbo].[Zone] ON");
_context.Zones.AddRange(final_zones);
_context.SaveChanges();
dbContextTransaction.Commit();
}
catch (Exception e)
{
dbContextTransaction.Rollback();
}
}
NOTE: You HAVE to do this per table. IDENTITY_INSERT can only be set for 1 table at a time it seems, so you can do it this way or toggle it to OFF in the same transaction.
Also, IDENTITY_INSERT has to be in a transaction, as it only stays on for the duration of a transaction.
The SET IDENTITY_INSERT
statement is scoped to one SQL session, which is practically equivalent to one set of statements that is sent over an open connection.
However, by default, an EF context will open and close a connection for each single statement with database interaction it executes. So after the ExecuteSqlCommand
statement, the connection is closed, and the IDENTITY_INSERT
is reset.
Now it's a somewhat hidden feature that EF won't close a connection if you open it before the context executes statements. So if you do this ...
try
{
_context.Database.Connection.Open();
_context.Database.ExecuteSqlCommand(@"SET IDENTITY_INSERT [CFETSWeb].[dbo].[Zone] ON");
_context.Zones.AddRange(final_zones);
_context.SaveChanges();
}
finally
{
_context.Database.Connection.Close();
}
... you'll notice that the IDENTITY_INSERT
setting will "survive" the ExecuteSqlCommand
statement.