众所周知在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. ubuntu 安装 pythonenv

    This will get you going with the latest version of pyenv and make it easy to fork and contribute any ...

  2. xampp的安装和配置

    这几天一直在做一个网站,客户要求要用PHP修改WordPress的themes,目的是交付完成后,客户自己管理方便. 以前从没有涉及过PHP,用的是jsp,但是,既然已经选择接受,就只能让自己去适应客 ...

  3. c# 去除字符串中重复字符

    String.Join 和 Distinct 方法 https://www.cnblogs.com/louby/p/6224960.html 1.在写程序中经常操作字符串,需要去重,以前我的用方式利用 ...

  4. ES6(四)字符串的扩展

    1.字符的表示方式 最早在  \u0000-\uFFFF 之间的字符已经足够使用吗,每个字符占两个字节,超出范围,必须使用双字节形式表达, 即每个字符占四个字节.超出范围的字符,会被解读成  \uXX ...

  5. Tomcat 到底依赖JRE还是JDK

    Tomcat 6.0 以上可以不再依赖JDK运行,直接使用JRE即可,但Tomcat 5.5以下,是必须安装JDK的. 这主要是由于,Tomcat 5.5及以下版本主要是依赖JDK去编译JSP文件生成 ...

  6. Head First设计模式之外观模式

    一.定义 外观模式提供了一个统一的接口,用来访问子系统中的一群接口.外观定义了一个高层接口,让子系统更容易使用. 外观模式不只是简化了接口,也将客户从组件的子系统中解耦. 外观和适配器可以包装许多类, ...

  7. docker for windows & dotnet core app

    Step 1: 安装docker for windows Step 2: 从github 上 clone 源代码:https://github.com/dotnet/dotnet-docker-sam ...

  8. 使用performance monitor 查看 每一个cpu core的cpu time

    使用performance monitor 查看 每一个cpu core的cpu time: 打开performance monitor,添加 counter 如下 运行一段cpu bound 的代码 ...

  9. JQuery基本语法(部分)

    1.jQuery介绍 jQuery 是一个 JavaScript 函数库. jQuery 库包含以下特性: HTML 元素选取 HTML 元素操作 CSS 操作 HTML 事件函数 JavaScrip ...

  10. C# log4net 的配置

    项目的日志组件是必备可少的,任何项目中都需要.这样既方便前期的开发测试也方便项目后期的项目维护.C#项目的一个不错的日志组件是log4net,下面我就把桌面应用程序.控制台程序.网站中log4net的 ...