众所周知在EF 6 及以前的版本中,是支持懒加载(Lazy Loading)的,可惜在EF Core 并不支持,必须使用Include方法来支持导航属性的数据加载。不过现在EF Core的开发团队打算恢复对这一功能的支持(目前还未发布,不过可以在Github上面下载进行测试)。

懒加载

懒加载也可以叫做按需加载、延迟加载。可以分两方面来理解,一方面指暂时不需要该数据,不用在当前马上加载,而可以推迟到使用它时再加载;另一方面指不确定是否将会需要该数据,所以暂时请不要加载,待确定需要后再加载它。懒加载是一种很重要的数据访问特性,可以有效地减少与数据源的交互(注意,这里所提的交互不是指交互次数,而是指交互的数据量),从而提升程序性能。

EF 6 懒加载

我们先来看一看在EF 6中的懒加载的使用方式。

实体定义:

    public class Order
{
public int OrderID { get; set; }
public string CustomerID { get; set; } public DateTime? OrderDate { get; set; } public virtual ICollection<OrderDetail> OrderDetails { get; set; }
} public class OrderDetail
{
public int OrderID { get; set; }
public int ProductID { get; set; }
public decimal UnitPrice { get; set; }
public short Quantity { get; set; }
public float Discount { get; set; }
public virtual Order Order { get; set; }
}

我们在这里定义订单订单明细实体,它们是一对多关系,通过OrderId字段进行关联。

        using (NorthwindContext context = new NorthwindContext()) {

            Order order = await context.Orders.SingleAsync(item => item.OrderID == 10253);

            Assert.NotNull(order);

            Assert.NotNull(order.OrderDetails);

            Assert.Equal(3, order.OrderDetails.Count);
}
}

在查询订单号为 10253 的订单后,如果我们需要访问订单的明细,不需要再编写一次数据查询的代码,直接访问导航属性即可,EF会自动帮我们填充属性的值。

懒加载需要注意以下两点:

  • 在配置中启用了懒加载(默认开启);
  • 实体类不能是封闭(sealed)类,导航属性必须是虚(virtual)属性。

在 EF Core 中启用懒加载

目前EF Core发布的最新版本中并不支持懒加载,开发人员必须使用Include方法,才能完成导航属性的加载。

        using (NorthwindContext context = new NorthwindContext()) {

            Order order = await context.Orders.Include(e => e.OrderDetails).SingleAsync(item => item.OrderID == 10253);

            Assert.NotNull(order);

            Assert.NotNull(order.OrderDetails);

            Assert.Equal(3, order.OrderDetails.Count);
}

大家需要在Github上面下载最新的源代码来测试这一功能 aspnet/EntityFrameworkCore

启用懒加载:

    public class NorthwindContext : DbContext
{
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
var sqlConnectionStringBuilder = new SqlConnectionStringBuilder {
DataSource = "****",
InitialCatalog = "Northwind",
UserID = "sa",
Password = "sa"
}; optionsBuilder.UseSqlServer(sqlConnectionStringBuilder.ConnectionString); optionsBuilder.UseLazyLoadingProxies(); base.OnConfiguring(optionsBuilder);
} }

要在通常的应用程序中使用,只需在DbContextOnConfiguring方法中添加对UseLazyLoadingProxies()扩展方法调用即可。

框架目前是通过Castle.Core框架来生成代理类来实现对导航属性的延迟加载,开发团队打算将该功能做为EF Core的可选安装包。

如果您对该功能感兴趣,可以在Github上面下载源代码进行测试。

Entity Framework Core 懒加载的更多相关文章

  1. Entity Framework 的懒加载、预先加载、显示加载

    1.新建两个实体,一个班级有多个学生 public class Student { public int StudentId { get; set; } public string StudentNa ...

  2. Entity Framework 一次加载许多个 Fluent API 映射

    可通过多种方法来指定模型的 Fluent 映射(从类到数据库). 1.直接在 DbContext 类的 OnModel­Creating 方法中进行映射,如下所示: protected overrid ...

  3. Entity Framework Core 2.1 Preview1 新增功能简介

    两个星期前,微软发布了EF Core 2.1 Preview 1,同时还发布了.NET Core 2.1 Preview 1和ASP.NET Core 2.1 Preview 1:EF Core 2. ...

  4. Spring Boot JPA Entity Jackson序列化触发懒加载的解决方案

    Spring Jpa这项技术在Spring 开发中经常用到. 今天在做项目用到了Entity的关联懒加载,但是在返回Json的时候,不管关联数据有没有被加载,都会触发数据序列化,而如果关联关系没有被加 ...

  5. 张高兴的 Entity Framework Core 即学即用:(一)创建第一个 EF Core 应用

    写在前面 Entity Framework Core (EF Core) 是 .NET 平台流行的对象关系映射(ORM)框架.虽然 .NET 平台中 ORM 框架有很多,比如 Dapper.NHibe ...

  6. Entity Framework Core 1.1 升级通告

    原文地址:https://blogs.msdn.microsoft.com/dotnet/2016/11/16/announcing-entity-framework-core-1-1/ 翻译:杨晓东 ...

  7. Entity Framework Core 1.1 Preview 1 简介

    实体框架核心(EF Core)是Entity Framework的一个轻量级,可扩展和跨平台版本. 10月25日,Entity Framework Core 1.1 Preview 1发布了. 升级到 ...

  8. Working with Data » Getting started with ASP.NET Core and Entity Framework Core using Visual Studio » 更新关系数据

    Updating related data¶ 7 of 7 people found this helpful The Contoso University sample web applicatio ...

  9. Working with Data » Getting started with ASP.NET Core and Entity Framework Core using Visual Studio » 读取关系数据

    Reading related data¶ 9 of 9 people found this helpful The Contoso University sample web application ...

随机推荐

  1. 初探XRebel

    一.什么是XRebel? 1.介绍 XRebel 是不间断运行在 web 应用的交互式分析器.可以看到网页上的每一个操作在前端以及服务端.数据库.网络传输都花费多少时间,当发现问题会在浏览器中显示警告 ...

  2. 深入理解用户权限rwx

    其实在UNIX的实现中,文件权限用12个二进制位表示,如果该位置上的值是1,表示有相应的权限,如果是0则没有相应权限第11位为SUID位,第10位为SGID位,第9位为sticky位,第8-0位对应于 ...

  3. 解决mysql漏洞 Oracle MySQL Server远程安全漏洞(CVE-2015-0411)

    有时候会检测到服务器有很多漏洞,而大部分漏洞都是由于服务的版本过低的原因,因为官网出现漏洞就会发布新版本来修复这个漏洞,所以一般情况下,我们只需要对相应的软件包进行升级到安全版本即可. 通过查阅官网信 ...

  4. DCL的失效:现实与初衷的背离

    最近看了Brian Goetz写的一篇有关DCL的文章:Double-checked locking: Clever, but broken.( 2001年发表于JavaWorld上) 这篇文章讲述了 ...

  5. MySQL小抄

    以下是MySQL5.7中的一些tips&tricks(持续更新中): Use of an unqualified * with other items in the select list m ...

  6. 佛祖保佑永无bug的源代码

    ${AnsiColor.BRIGHT_YELLOW} ${AnsiColor.BRIGHT_RED}_ooOoo_${AnsiColor.BRIGHT_YELLOW} ${AnsiColor.BRIG ...

  7. 【http转https】其之二:申请Let's Encrypt颁发SSL证书

    文:铁乐猫 2017年1月12日 申请Let's Encrypt颁发SSL证书 由 ISRG(Internet Security Research Group,互联网安全研究小组)提供服务, ISRG ...

  8. crontabs linux定时任务功能安装

    crontab命令常见于Unix和Linux的操作系统之中,用于设置周期性被执行的指令.该命令从标准输入设备读取指令,并将其存放于"crontab"文件中,以供之后读取和执行.通常 ...

  9. TurnipBit之DIY无线遥控智能小车

    一.准备工作 TurnipBit 开发板 2块 TurnipBit 扩展板 1块 数据线 1条 智能小车器件 1套 电机驱动模块(L298N) 1个 在线可视化编程 点击进入   二.思路设计   2 ...

  10. k8s 重要概念 - 每天5分钟玩转 Docker 容器技术(117)

    在实践之前,必须先学习 Kubernetes 的几个重要概念,它们是组成 Kubernetes 集群的基石. Cluster Cluster 是计算.存储和网络资源的集合,Kubernetes 利用这 ...