实体框架-使用包含/渴望加载和延迟加载有什么区别? [英] Entity Framework - what's the difference between using Include/eager loading and lazy loading?

查看:61
本文介绍了实体框架-使用包含/渴望加载和延迟加载有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直试图使自己熟悉实体框架.大部分看起来很简单,但是我对使用Include方法进行的快速加载与默认的延迟加载之间的区别感到有些困惑.两者似乎都在加载相关的实体,因此从表面上看,它们似乎在做相同的事情.我想念什么?

I've been trying to familiarize myself with the Entity Framework. Most of it seems straight forward, but I'm a bit confused on the difference between eager loading with the Include method and default lazy loading. Both seem like they load related entities, so on the surface it looks like they do the same thing. What am I missing?

推荐答案

假设您有两个具有一对多关系的实体:客户和订单,每个客户可以有多个订单.

Let's say you have two entities with a one-to-many relationship: Customer and Order, where each Customer can have multiple Orders.

在加载客户实体时,实体框架允许您急于加载或延迟加载客户的订单集合.如果选择急于加载Orders集合,则当您从数据库中检索客户时,Entity Framework将生成SQL,该SQL在一个查询中同时检索客户信息和客户订单.但是,如果您选择延迟加载Orders集合,则当您从数据库中检索客户时,实体框架将生成仅 拉取客户信息的SQL(如果出现以下情况,实体框架将生成单独的SQL语句:您稍后将在代码中访问客户的订单"集合.

When loading up a Customer entity, Entity Framework allows you to either eager load or lazy load the Customer's Orders collection. If you choose to eager load the Orders collection, when you retrieve a Customer out of the database Entity Framework will generate SQL that retrieves both the Customer's information and the Customer's Orders in one query. However, if you choose to lazy load the Orders collection, when you retrieve a Customer out of the database Entity Framework will generate SQL that only pulls the Customer's information (Entity Framework will then generate a separate SQL statement if you access the Customer's Orders collection later in your code).

确定何时使用紧急加载和何时使用延迟加载,所有这些都取决于您希望对检索到的实体执行的操作.如果您知道只需要客户的信息,则应该延迟加载Orders集合(这样,仅通过检索客户的信息,SQL查询就可以变得高效).相反,如果您知道需要遍历客户的订单,则应该急于加载订单(这样,一旦您在代码中访问客户的订单,就可以节省额外的数据库命中次数).

Determining when to use eager loading and when to use lazy loading all comes down to what you expect to do with the entities you retrieve. If you know you only need a Customer's information, then you should lazy-load the Orders collection (so that the SQL query can be efficient by only retrieving the Customer's information). Conversely, if you know you'll need to traverse through a Customer's Orders, then you should eager-load the Orders (so you'll save yourself an extra database hit once you access the Customer's Orders in your code).

P.S.使用延迟加载时要非常小心,因为它可能导致N + 1问题.例如,假设您有一个页面,显示一个客户及其订单"列表.但是,您决定在获取订单时使用延迟加载.当您遍历Customer集合,然后遍历每个Customer的Orders时,您将为每个Customer执行数据库命中,以延迟加载到他们的Orders集合中.这意味着对于N个客户,您将拥有N + 1个数据库命中(1个数据库命中可加载所有客户,然后N个数据库命中可加载其每个订单),而不是只有1个数据库命中率(可以在一个查询中检索到所有客户及其订单).

P.S. Be very careful when using lazy-loading as it can lead to the N+1 problem. For example, let's say you have a page that displays a list of Customers and their Orders. However, you decide to use lazy-loading when fetching the Orders. When you iterate over the Customers collection, then over each Customer's Orders, you'll perform a database hit for each Customer to lazy-load in their Orders collection. This means that for N customers, you'll have N+1 database hits (1 database hit to load up all the Customers, then N database hits to load up each of their Orders) instead of just 1 database hit had you used eager loading (which would have retrieved all Customers and their Orders in one query).

这篇关于实体框架-使用包含/渴望加载和延迟加载有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆