Entity Framework主從表的增刪改
一、添加數(shù)據(jù)
1、在主表中添加從表數(shù)據(jù)
在景點(diǎn)的住宿集合(Lodgings)中增加一個度假區(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ù)據(jù)
添加一個帶兩個住宿的景點(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ù)據(jù)
添加一個帶有景點(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、刪除主表的同時刪除相關(guān)聯(lián)的從表數(shù)據(jù)(級聯(lián)刪除)
如果數(shù)據(jù)庫里設(shè)置是級聯(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ù),同時標(biāo)注從表數(shù)據(jù)為刪除狀態(tài)(數(shù)據(jù)庫關(guān)閉了級聯(lián)刪除的情況,可以手動去數(shù)據(jù)庫的外鍵關(guān)系修改,也可以Fluent API配置關(guān)閉級聯(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主從表增刪改的文章就介紹到這了。希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
在Winform和WPF中注冊全局快捷鍵實(shí)現(xiàn)思路及代碼
如果注冊快捷鍵,RegisterHotKey中的fsModifiers參數(shù)為0,即None選項(xiàng),一些安全軟件會警報(bào),可能因?yàn)檫@樣就可以全局監(jiān)聽鍵盤而造成安全問題,感興趣的你可以參考下本文2013-02-02
C#設(shè)計(jì)模式之Observer觀察者模式解決牛頓童鞋成績問題示例
這篇文章主要介紹了C#設(shè)計(jì)模式之Observer觀察者模式解決牛頓童鞋成績問題,簡單講述了觀察者模式的原理并結(jié)合具體實(shí)例形式分析了使用觀察者模式解決牛頓童鞋成績問題的具體步驟相關(guān)操作技巧,并附帶demo源碼供讀者下載參考,需要的朋友可以參考下2017-09-09
C#中加載dll并調(diào)用其函數(shù)的實(shí)現(xiàn)方法
下面小編就為大家?guī)硪黄狢#中加載dll并調(diào)用其函數(shù)的實(shí)現(xiàn)方法。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-02-02
C#調(diào)用windows api關(guān)機(jī)(關(guān)機(jī)api)示例代碼分享
本文主要介紹了C#調(diào)用windows api關(guān)機(jī)的示例代碼,大家參考使用吧2014-01-01
C# WinForm制作一個批量轉(zhuǎn)化文件格式的小工具
在生活中有時候會遇到批量轉(zhuǎn)換格式的需求,一個個點(diǎn)太麻煩了,一個能夠?qū)崿F(xiàn)批量文件格式轉(zhuǎn)換的工具非常有用,所以本文小編使用C# WinForm制作一個批量轉(zhuǎn)化文件格式的小工具,文中有具體實(shí)現(xiàn)代碼,需要的朋友可以參考下2023-11-11

