Entity Framework主從表的增刪改
一、添加數(shù)據(jù)
1、在主表中添加從表數(shù)據(jù)
在景點(diǎn)的住宿集合(Lodgings)中增加一個(gè)度假區(qū)(Resort)
var dest = (from d in context.Destinations where d.Name == "Bali" select d).Single(); var resort = new CodeFirst.Model.Resort { Name = "Pete's Luxury Resort", }; dest.Lodgings.Add(resort); context.SaveChanges();
2、添加主表的同時(shí)添加從表數(shù)據(jù)
添加一個(gè)帶兩個(gè)住宿的景點(diǎn)
var destination = new CodeFirst.Model.Destination { Name = "AnHui HuangShan", Lodgings = new List { new CodeFirst.Model.Lodging {Name="HuangShan Hotel"}, new CodeFirst.Model.Lodging {Name="YingKeSong Hotel"} } }; context.Destinations.Add(destination); context.SaveChanges();
3、添加從表的同時(shí)添加主表數(shù)據(jù)
添加一個(gè)帶有景點(diǎn)信息度假村到住宿信息中。
var resort = new CodeFirst.Model.Resort { Name = "Top Notch Resort and Spa", Destination = new CodeFirst.Model.Destination { Name = "Stowe, Vermont", Country = "USA" } }; using (var context = new CodeFirst.DataAccess.BreakAwayContext()) { context.Lodgings.Add(resort); context.SaveChanges(); }
二、修改關(guān)聯(lián)
1、修改從表的外鍵
var hotel = (from l in context.Lodgings where l.Name == "YingKeSong Hotel" select l).Single(); var reef = (from d in context.Destinations where d.Name == "Bali" select d).Single(); hotel.Destination = reef; context.SaveChanges();
2、從表與主表脫離關(guān)系
1、ForeignKeys方式:
var davesDump = (from l in context.Lodgings where l.Name == "HuangShan Hotel" select l).Single(); davesDump.DestinationID = null;//(ForeignKeys方式) context.SaveChanges();
2、Reference方式:
var davesDump = (from l in context.Lodgings where l.Name == "HuangShan Hotel" select l).Single(); context.Entry(davesDump).Reference(l => l.Destination).Load(); //找主表數(shù)據(jù) davesDump.Destination = null; //清空,(Reference方式) context.SaveChanges();
三、刪除關(guān)聯(lián)數(shù)據(jù)
1、刪除主表的同時(shí)刪除相關(guān)聯(lián)的從表數(shù)據(jù)(級(jí)聯(lián)刪除)
如果數(shù)據(jù)庫里設(shè)置是級(jí)聯(lián)刪除,則不顯示加載從表數(shù)據(jù)。
var canyon = (from d in context.Destinations where d.Name == "AnHui HuangShan" select d).Single(); context.Entry(canyon).Collection(d => d.Lodgings).Load(); //從表顯示加載后,再刪除主表數(shù)據(jù) context.Destinations.Remove(canyon); context.SaveChanges();
2、普通刪除
刪除主表數(shù)據(jù),同時(shí)標(biāo)注從表數(shù)據(jù)為刪除狀態(tài)(數(shù)據(jù)庫關(guān)閉了級(jí)聯(lián)刪除的情況,可以手動(dòng)去數(shù)據(jù)庫的外鍵關(guān)系修改,也可以Fluent API配置關(guān)閉級(jí)聯(lián)刪除)
var canyon = (from d in context.Destinations where d.Name == "Grand Canyon" select d).Single(); foreach (var lodging in canyon.Lodgings.ToList()) { context.Lodgings.Remove(lodging); //先標(biāo)記相關(guān)的從表數(shù)據(jù)為刪除狀態(tài) } context.Destinations.Remove(canyon); //再標(biāo)記主表數(shù)據(jù)為刪除裝填 context.SaveChanges(); //執(zhí)行上面的所有標(biāo)記
到此這篇關(guān)于Entity Framework主從表增刪改的文章就介紹到這了。希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
基于C#解決庫存扣減及訂單創(chuàng)建時(shí)防止并發(fā)死鎖的問題
這篇文章主要介紹了基于C#解決庫存扣減及訂單創(chuàng)建時(shí)防止并發(fā)死鎖的問題,很多開發(fā)人員對(duì)于這個(gè)問題的排查起來是比較困難的,而生產(chǎn)生的原因多種多樣,很多人認(rèn)是因?yàn)楸碇械臄?shù)據(jù)太多了同時(shí)操作的人多人才會(huì)產(chǎn)生這種錯(cuò)誤,下面我們來還原一下死鎖的過程2022-05-05c#中WinForm使用OpencvSharp4實(shí)現(xiàn)簡易抓邊
本文主要介紹了c#中WinForm使用OpencvSharp4實(shí)現(xiàn)簡易抓邊,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-05-05C#解析char型指針?biāo)赶虻膬?nèi)容(實(shí)例解析)
在c++代碼中定義了一個(gè)功能函數(shù),這個(gè)功能函數(shù)會(huì)將計(jì)算的結(jié)果寫入一個(gè)字符串型的數(shù)組中output,然后c#會(huì)調(diào)用c++導(dǎo)出的dll中的接口函數(shù),然后獲取這個(gè)output并解析成string類型,本文通過實(shí)例解析C#?char型指針?biāo)赶虻膬?nèi)容,感興趣的朋友一起看看吧2024-03-03UnityWebRequest前后端交互實(shí)現(xiàn)過程解析
這篇文章主要介紹了UnityWebRequest前后端交互實(shí)現(xiàn)過程解析,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-06-06Unity實(shí)現(xiàn)毫秒延時(shí)回調(diào)功能
這篇文章主要為大家詳細(xì)介紹了Unity實(shí)現(xiàn)毫秒延時(shí)回調(diào)功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-09-09