Entity Framework Core - Cannot insert explicit value when saving to another table

asp.net-core asp.net-core-mvc entity-framework-core sql sqlexception

Popular Answer

IDbContextTransaction trn = testDBContext.Database.BeginTransaction();
Item itm = new Item();
itm.ItemId = 23;
itm.ItemName = "item23";
Type typt = typeof(Item);
DbSet<Item> Item1 = testDBContext.Set<Item>();
testDBContext.Database.ExecuteSqlCommand("SET IDENTITY_INSERT [dbo].[Item] ON");
//Item1.FromSql("SET IDENTITY_INSERT [dbo].[Item] ON").SingleOrDefaultAsync();

testDBContext.Item.Add(itm);
testDBContext.SaveChanges(); 

//Item1.FromSql("SET IDENTITY_INSERT [dbo].[Item] OFF").SingleOrDefaultAsync();
testDBContext.Database.ExecuteSqlCommand("SET IDENTITY_INSERT [dbo].[Item] OFF");
trn.Commit();
trn.Dispose();
0
4/17/2019 10:25:46 AM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow