Entity Framework主从表数据加载方式
作者:springsnow 发布时间:2022-03-10 21:44:14
标签:Entity,Framework,主从表,数据,加载,方式
一、延迟加载: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
0
投稿
猜你喜欢
- 目录1. SpringCloud特点2. 分布式系统的三个指标CAP3. Eureka4. SpringCloud Demo4.1 regi
- Spring整合mybatis的mapper生成过程mapperScannerConfigurer实现了BeandifinitionRegi
- 一、项目简述功能包括(管理员和学生角色): 管理员和学生登录,图书管理,图书添加删除修改,图书 借阅,图书归还,图书查看,学生管理,借还管理
- 本文实例讲述了C#4.0新特性的协变与逆变,有助于大家进一步掌握C#4.0程序设计。具体分析如下:一、C#3.0以前的协变与逆变如果你是第一
- MyBatis-Plus是通过version机制实现乐观锁的。大致思路:取出记录,携带记录的当前version;更新记录的时候,比较记录当前
- 一、简介   Seata 是一款开源的分布式事务解决方案,致力于在微服务架构下提供高性能和简单易用的分布
- SpringBoot对actuator进行关闭management: endpoint: health
- 本文实例为大家分享了Spring MVC多文件上传的具体代码,供大家参考,具体内容如下1)创建工程并导入JAR包2)创建多文件选择页面在 W
- 前言本身我是一个比较偏向少使用Stream的人,因为调试比较不方便。但是, 不得不说,stream确实会给我们编码带来便捷。Stream流
- springmvc @RequestBody String类型参数通过如下配置: <bean id="mapp
- 前导:发过程中经常会使用java将office系列文档转换为PDF, 一般都使用微软提供的openoffice+jodconverter 实
- 本文实例讲述了Java类加载器和类加载机制。分享给大家供大家参考,具体如下:一 点睛1 类加载器负责将.class文件(可能在磁盘上,也可能
- 定义里氏替换原则(Liskov Substitution Principle,LSP),官方定义如下: 如果对每一个类型为S的对象o1,都有
- Oracle官网是这样介绍默认方法的,使用默认方法,可以达到往接口里面增加新的功能,而且保持与老版本代码兼容,也就是原来的实现类可以不需要被
- 一、项目运行环境配置:Jdk1.8 + Tomcat8.5 + mysql + Eclispe(IntelliJ IDEA,Eclispe,
- 本文实例为大家分享了Unity实现俄罗斯方块第一部分,供大家参考,具体内容如下准备工作1、新建一个2D项目,新建成功以后设置相机的一些参数2
- 创建类第一步新建一个java类QSV,构造函数传入需要解析的文件名称。public class QSV {private RandomAcc
- 前言我们在日常的开发过程中针对一些字段采用整型的方式去代替某些具体的含义,比如性别0代表男,1代表女。如果只是一些不会变更的转译我们可以采用
- 很多学习Android程序设计的人都会发现每个人对代码的写法都有不同的偏好,比较明显的就是对控件响应事件的写法的不同。因此本文就把这些写法总
- 理解枚举类型枚举类型是Java 5中新增特性的一部分,它是一种特殊的数据类型,之所以特殊是因为它既是一种类(class)类型却又比类类型多了