[EntityFramework] 如何cascade insert

當在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");
}

 

About C.H. Ling 262 Articles
a .net / Java developer from Hong Kong and currently located in United Kingdom. Thanks for Google because it solve many technical problems so I build this blog as return. Besides coding and trying advance technology, hiking and traveling is other favorite to me, so I will write down something what I see and what I feel during it. Happy reading!!!

Be the first to comment

Leave a Reply

Your email address will not be published.


*


This site uses Akismet to reduce spam. Learn how your comment data is processed.