2015/06/12

How to perform SET IDENTITY INSERT ON from Entity Framework ?

using (TransactionScope scope = new TransactionScope(TransactionScopeOption.Required))
{
using (var conn = new System.Data.SqlClient.SqlConnection(_connectionstring))
{
conn.Open();
using (Context context = new Context(conn, false))
{
context.Database.ExecuteSqlCommand(@"SET IDENTITY_INSERT [dbo].[TableName] ON");
context.TableName.AddRange(items);
context.SaveChanges();
context.Database.ExecuteSqlCommand(@"SET IDENTITY_INSERT [dbo].[TableName] OFF");
scope.Complete();
}
}
}
view raw gistfile1.cs hosted with ❤ by GitHub
Thanks to: http://blog.robertobonini.com/2014/10/09/entity-framework-with-identity-insert/

No comments:

Post a Comment