Entity Framework Core 懒加载
众所周知在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);
}
}
要在通常的应用程序中使用,只需在DbContext的OnConfiguring方法中添加对UseLazyLoadingProxies()扩展方法调用即可。
框架目前是通过Castle.Core框架来生成代理类来实现对导航属性的延迟加载,开发团队打算将该功能做为EF Core的可选安装包。
如果您对该功能感兴趣,可以在Github上面下载源代码进行测试。
Entity Framework Core 懒加载的更多相关文章
- Entity Framework 的懒加载、预先加载、显示加载
1.新建两个实体,一个班级有多个学生 public class Student { public int StudentId { get; set; } public string StudentNa ...
- Entity Framework 一次加载许多个 Fluent API 映射
可通过多种方法来指定模型的 Fluent 映射(从类到数据库). 1.直接在 DbContext 类的 OnModelCreating 方法中进行映射,如下所示: protected overrid ...
- 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. ...
- Spring Boot JPA Entity Jackson序列化触发懒加载的解决方案
Spring Jpa这项技术在Spring 开发中经常用到. 今天在做项目用到了Entity的关联懒加载,但是在返回Json的时候,不管关联数据有没有被加载,都会触发数据序列化,而如果关联关系没有被加 ...
- 张高兴的 Entity Framework Core 即学即用:(一)创建第一个 EF Core 应用
写在前面 Entity Framework Core (EF Core) 是 .NET 平台流行的对象关系映射(ORM)框架.虽然 .NET 平台中 ORM 框架有很多,比如 Dapper.NHibe ...
- Entity Framework Core 1.1 升级通告
原文地址:https://blogs.msdn.microsoft.com/dotnet/2016/11/16/announcing-entity-framework-core-1-1/ 翻译:杨晓东 ...
- Entity Framework Core 1.1 Preview 1 简介
实体框架核心(EF Core)是Entity Framework的一个轻量级,可扩展和跨平台版本. 10月25日,Entity Framework Core 1.1 Preview 1发布了. 升级到 ...
- 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 ...
- 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 ...
随机推荐
- Func和Action委托简单用法
Func和Action类是特殊的类型,它们允许你在不必指定自定义委托类型的情况下,去使用委托.在整个.NET框架中都可以使用它们.例如,在我们考察并行计算时,你也会看到这两个类的示例. 上面一段文字是 ...
- 微信扫码支付功能详细教程————Java
前言 首先声明 我并非原创 原创是 http://blog.csdn.net/wangqiuyun/article/details/51241064 我只是在前辈的基础 加以解释说明 还有自己的一些 ...
- JavaScript的DOM编程--12--innerHTML属性
innerHTML属性: 1). 浏览器几乎都支持该属性, 但不是 DOM 标准的组成部分. innerHTML 属性可以用来读, 写某给定元素里的 HTML 内容 <html> < ...
- struts快速入门第一篇 —— struts相关XML配置映射及讲解
我们回忆一下在学习JavaWeb过程中(Jsp + servlet编程)所感受到的Servlet的不足: 1 Servllet很多时,web.xml中的代码会很多.这样一来,维护起来就不方便,不利于团 ...
- Python+selenium+eclipse+pydev自动化测试环境搭建
一. 安装python 1.下载安装python 可访问python的官方网站:http://www.Python.prg找到下载页面下载需要的版本,可下载python2.x或者pyth ...
- IndentationError: unexpected indent
都知道python是对格式要求很严格的,写了一些python但是也没发现他严格在哪里,今天遇到了IndentationError: unexpected indent错误我才知道他是多么的严格. ...
- CSS Sprites使用
CSS Sprites在国内很多人叫css精灵,是一种网页图片应用处理方式.它允许你将一个页面涉及到的所有零星图片都包含到一张大图中去,这样一来,当访问该页面时,载入的图片就不会像以前那样一幅一幅地慢 ...
- Web App、Hybrid App与Native App
在这个App的时代,转战了前端,一直接触的都是pc, 离out不远了. 那么接下来,app是我接下来半年的重点,为什么是半年,因为时间不多了. 因为是前端,那么我的重心肯定是 Web App, Hyb ...
- jBPM学习之利用API完成流程实例
流程引擎对象ProcessEngine是jBPM4所有的Service API之源.在jBPM4中各种服务相互依存,但所有的Service API都从ProcessEngine中获得,由此可见Proc ...
- ATM机运行代码
实现代码: import java.util.Scanner; public class Atm { public static void main(String[] args) { // TODO ...