網站首頁 編程語言 正文
一、延遲加載:LazyLoading
使用延遲加載,關聯的實體必須標注為virtual。
本例是標注Destination類里的Lodgings為virtual。因為先發sql去查詢主鍵對象,然后根據主鍵id去從表里查相關聯的數據。
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);
}
}
改進:在數據庫中操作,顯示加載
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();//接下來的查詢在數據庫中,包括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、查找導航屬性為一個集合的
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、查找導航屬性為一個實體對象的
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) //遍歷的是內存中的Destinations數據
Console.WriteLine(destination.Name);
}
}
原文鏈接:https://www.cnblogs.com/springsnow/p/13251419.html
相關推薦
- 2022-08-21 Python?lambda函數保姆級使用教程_python
- 2022-07-11 pandas的排序、分組groupby及cumsum累計求和方式_python
- 2022-04-03 .NET?Core配置連接字符串和獲取數據庫上下文實例_實用技巧
- 2022-06-06 Array.prototype.myjoin
- 2022-11-06 pytorch簡單實現神經網絡功能_python
- 2022-04-18 Python?socket如何解析HTTP請求內容_python
- 2022-07-14 Nginx限流和黑名單配置的策略_nginx
- 2022-09-30 react?redux的原理以及基礎使用講解_React
- 最近更新
-
- 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同步修改后的遠程分支