網(wǎng)站首頁 編程語言 正文
一、延遲加載:LazyLoading
使用延遲加載,關(guān)聯(lián)的實(shí)體必須標(biāo)注為virtual。
本例是標(biāo)注Destination類里的Lodgings為virtual。因?yàn)橄劝l(fā)sql去查詢主鍵對(duì)象,然后根據(jù)主鍵id去從表里查相關(guān)聯(lián)的數(shù)據(jù)。
private static void TestLazyLoading()
{
using (var context = new CodeFirst.DataAccess.BreakAwayContext())
{
var canyon = (from d in context.Destinations where d.Name == "Grand Canyon" select d).Single();
var distanceQuery = from l in canyon.Lodgings //延遲加載canyon的所有.Lodgings
where l.Name == "HuangShan Hotel"
select l;
foreach (var lodging in distanceQuery)
Console.WriteLine(lodging.Name);
}
}
改進(jìn):在數(shù)據(jù)庫中操作,顯示加載
private static void QueryLodgingDistancePro()
{
using (var context = new CodeFirst.DataAccess.BreakAwayContext())
{
var canyon = (from d in context.Destinations where d.Name == "Grand Canyon" select d).Single();
var lodgingQuery = context.Entry(canyon).Collection(d => d.Lodgings).Query();//接下來的查詢?cè)跀?shù)據(jù)庫中,包括Count()等
var distanceQuery = from l in lodgingQuery
where l.Name == "HuangShan Hotel"
select l;
foreach (var lodging in distanceQuery)
Console.WriteLine(lodging.Name);
}
}
二、貪婪加載:EagerLoading
private static void TestEagerLoading()
{
using (var context = new CodeFirst.DataAccess.BreakAwayContext())
{
// var allDestinations = context.Destinations.Include(d => d.Lodgings);
var AustraliaDestination = context.Destinations.Include(d => d.Lodgings).Where(d => d.Name == "Bali");
//context.Lodgings.Include(l => l.PrimaryContact.Photo);
//context.Destinations.Include(d => d.Lodgings.Select(l => l.PrimaryContact));
//context.Lodgings.Include(l => l.PrimaryContact).Include(l => l.SecondaryContact);
foreach (var destination in AustraliaDestination)
{
foreach (var lodging in destination.Lodgings)
Console.WriteLine(" - " + lodging.Name);
}
}
}
三、顯示加載:ExplicitLoading
1、查找導(dǎo)航屬性為一個(gè)集合的
private static void LoadRelateData()
{
using (var context = new CodeFirst.DataAccess.BreakAwayContext())
{
var canyon = (from d in context.Destinations where d.Name == "Grand Canyon" select d).Single();
context.Entry(canyon).Collection(d => d.Lodgings).Load(); //顯示加載
foreach (var lodging in context.Lodgings.Local)
Console.WriteLine(lodging.Name);
}
}
2、查找導(dǎo)航屬性為一個(gè)實(shí)體對(duì)象的
private static void LoadPrimaryKeyData()
{
using (var context = new CodeFirst.DataAccess.BreakAwayContext())
{
var lodging = context.Lodgings.First();
context.Entry(lodging).Reference(l => l.Destination).Load();
foreach (var destination in context.Destinations.Local) //遍歷的是內(nèi)存中的Destinations數(shù)據(jù)
Console.WriteLine(destination.Name);
}
}
原文鏈接:https://www.cnblogs.com/springsnow/p/13251419.html
相關(guān)推薦
- 2022-07-26 arduino上傳程序出錯(cuò)不成功常見的問題解決
- 2023-02-01 Python中使用zip函數(shù)的七重境界解析_python
- 2022-07-11 Sonatype Nexus搭建Maven私服
- 2022-07-09 Android實(shí)現(xiàn)app開機(jī)自啟動(dòng)功能_Android
- 2022-11-25 ASP.NET?MVC使用異步Action的方法_實(shí)用技巧
- 2022-06-20 基于C#實(shí)現(xiàn)語音識(shí)別功能詳解_C#教程
- 2022-08-17 jQuery實(shí)現(xiàn)購物車_jquery
- 2022-06-19 Go中Writer和Reader接口的使用入門_Golang
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲(chǔ)小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯(cuò)誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡(jiǎn)單動(dòng)態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對(duì)象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支