當在create model 時, 有機會assign value 到其相對應的class中之後做insert 動作. 但若在ORM 層面的話, 須要在所有child item inserted 後才可進行, 否則有機會throw exception. 為了應對這問題, 可以用以下方法做work-around:
using (NotificationEntities entites = new NotificationEntities())
{
entites.EMAILs.Add(email);
entites.Database.ExecuteSqlCommand("SET IDENTITY_INSERT [dbo].[EMAIL] ON");
entites.SaveChanges();
entites.Database.ExecuteSqlCommand("SET IDENTITY_INSERT [dbo].[EMAIL] OFF");
}
Leave a Reply