简体   繁体   English

EF渴望加载和延迟加载的区别?

[英]EF difference in eager loading and lazy loading?

I'm new to EF and have read some articles. 我是EF的新手,已经阅读了一些文章。 After reading I got confused in what is difference between lazy loading and eager loading? 阅读后,我对延迟加载和渴望加载之间的区别感到困惑?

  • Can both type of queries be compiled? 两种类型的查询都可以编译吗?
  • Can both type of queries return IQuerable and IEnumerable ? 两种查询都可以返回IQuerableIEnumerable吗?
  • Can both type of queries have Linq to Entities query syntex (select, from, where) and lambda expressions? 两种类型的查询都可以具有Linq to Entities查询语法(选择,从,哪里)和lambda表达式吗?

Kindly guide and help me. 请指导和帮助我。

Thanks a lot for your time and guidance 非常感谢您的时间和指导

About the difference between lazy and eager loading: 关于延迟加载和渴望加载之间的区别:

Suppose you have a Customer object with has a property List<Invoice> Invoices (which is in a different table and joined automatically by the Entity Framework). 假设您有一个Customer对象,该对象的属性为List<Invoice> Invoices (位于另一个表中,并由Entity Framework自动加入)。

With lazy loading the invoices won't be fetched immediately when your Customer object is instantiated, but only when you need it / access it explicitely. lazy加载不会在实例化Customer对象时立即获取发票,而是仅在需要时/显式访问发票。

With eager loading your invoices will be fetched and constructed / filled on the object immediately (which introduces some unnecessary performance overhead if you build a huge list of Customers but don't really need all the invoices to be readily available on the object). eager加载,您的发票将立即被提取并构造/填充到该对象上(如果您构建了大量的客户列表,但实际上并不需要所有发票都可以在该对象上使用,这会带来不必要的性能开销)。

You'll find some documentation here . 您可以在此处找到一些文档。

Can both type of queries be compiled? 两种类型的查询都可以编译吗?

Only eager loading can be part of manually pre-compiled query. 只有急切的加载才能成为手动预编译查询的一部分。 Lazy loading query is automatically created by EF and it is EF internal behavior if it actually pre-compiles it. 延迟加载查询由EF自动创建,并且如果它实际上是预先编译的,则它是EF内部行为。

Can both type of queries return IQuerable and IEnumerable? 两种查询都可以返回IQuerable和IEnumerable吗?

In eager loading you can control if the query returns IQueryable or IEnumerable . 在急切加载中,您可以控制查询是否返回IQueryableIEnumerable Lazy loading query happens out of your control and you cannot modify it. 延迟加载查询发生在您的控制范围之外,您无法对其进行修改。 If you want to use IQueryable for lazily loaded navigation properties you must use third option called explicit loading where you get IQueryable query for given navigation property and you can modify it. 如果要对延迟加载的导航属性使用IQueryable ,则必须使用称为显式加载的第三个选项,在该选项中,您可以获取给定导航属性的IQueryable查询,然后可以对其进行修改。

Can both type of queries have Linq to entities queries syntax (select, from, where) and lambda expressions? 两种查询都可以具有Linq to实体查询语法(选择,从,哪里)和lambda表达式吗?

No. Neither of these queries have select, from, where. 否。这两个查询都没有选择,从何处选择。 Lazy loading happens out of your control and eager loading doesn't allow filtering - in both cases you always load all related objects. 延迟加载发生在您的控制之外,并且急切的加载不允许过滤-在两种情况下,您始终都加载所有相关的对象。

Example of explicit query (the only type of loading where you can use query): 显式查询的示例(唯一可以使用查询的加载类型):

var query = ((EntityCollection<MyEntity>)parent.Children).CreateSourceQuery()
                                                         .Where(...);

Lazy loading is only loading related objects at the moment this is actually needed, eager loading is the opposite of this. 延迟加载仅在实际需要时才加载相关对象,而积极加载则与此相反。 The choice of strategy could have a big impact on performance, eg loading a big dataset when you don't actually need it. 策略的选择可能会对性能产生重大影响,例如,在您实际不需要时加载大型数据集。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM