網站首頁 編程語言 正文
一、添加數據
1、在主表中添加從表數據
在景點的住宿集合(Lodgings)中增加一個度假區(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、添加主表的同時添加從表數據
添加一個帶兩個住宿的景點
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、添加從表的同時添加主表數據
添加一個帶有景點信息度假村到住宿信息中。
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();
}
二、修改關聯
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、從表與主表脫離關系
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(); //找主表數據
davesDump.Destination = null; //清空,(Reference方式)
context.SaveChanges();
三、刪除關聯數據
1、刪除主表的同時刪除相關聯的從表數據(級聯刪除)
如果數據庫里設置是級聯刪除,則不顯示加載從表數據。
var canyon = (from d in context.Destinations where d.Name == "AnHui HuangShan" select d).Single();
context.Entry(canyon).Collection(d => d.Lodgings).Load(); //從表顯示加載后,再刪除主表數據
context.Destinations.Remove(canyon);
context.SaveChanges();
2、普通刪除
刪除主表數據,同時標注從表數據為刪除狀態(數據庫關閉了級聯刪除的情況,可以手動去數據庫的外鍵關系修改,也可以Fluent API配置關閉級聯刪除)
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); //先標記相關的從表數據為刪除狀態
}
context.Destinations.Remove(canyon); //再標記主表數據為刪除裝填
context.SaveChanges(); //執行上面的所有標記
原文鏈接:https://www.cnblogs.com/springsnow/p/13230080.html
相關推薦
- 2023-07-25 適用SpringMVC實現圖片上傳功能
- 2022-07-17 Android?studio實現簡單計算器的編寫_Android
- 2022-05-22 python_tkinter彈出對話框創建_python
- 2022-06-06 uniApp、uni.chooseLocation(OBJECT)、獲取位置、{errMsg: ‘g
- 2022-04-09 C語言實現計算器的兩種方法_C 語言
- 2022-12-11 C語言計算分段函數問題_C 語言
- 2024-03-22 【IDEA】Spring boot項目設置網站圖標favicon不生效
- 2023-02-02 一文教你利用Python制作一個生日提醒_python
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支