问题描述
我已阅读此 教程 和此 文章 但我不完全了解每种加载类型的用途.
I have read this tutorial and this article but I don't understand exactly the use of each loading type.
我解释
我有这个 POCO:
public partial class dpc_gestion
{
public dpc_gestion()
{
this.ass_reunion_participant = new HashSet<ass_reunion_participant>();
this.dpc_participant = new HashSet<dpc_participant>();
this.dpc_reunion = new HashSet<dpc_reunion>();
}
public int dpc_id_pk { get; set; }
public Nullable<int> dpc_id_gdp_fk { get; set; }
public Nullable<int> dpc_id_theme { get; set; }
public int dpc_id_animateur_fk { get; set; }
public Nullable<System.DateTime> dpc_date_creation { get; set; }
public Nullable<System.DateTime> dpc_date_fin { get; set; }
public Nullable<System.DateTime> dpc_date_engag_anim { get; set; }
public Nullable<bool> dpc_flg_let_engag_anim { get; set; }
public Nullable<bool> dpc_flg_fsoins_anim { get; set; }
public virtual ICollection<ass_reunion_participant> ass_reunion_participant { get; set; }
public virtual theme_dpc theme_dpc { get; set; }
public virtual gdp_groupe_de_pair gdp_groupe_de_pair { get; set; }
public virtual ICollection<dpc_participant> dpc_participant { get; set; }
public virtual ICollection<dpc_reunion> dpc_reunion { get; set; }
}
我明白了:
对于延迟加载:因为加载是延迟的,如果我调用 dbset
dpc_gestion
所有导航属性 不会加载.这种类型的加载在性能和响应能力方面是最好的.它默认启用,如果我想重新启用它,我必须设置:
For Lazy loading : because the load is lazy, if I call the dbset
dpc_gestion
all navigation properties won't be loaded. This type of loading is the best in performance and responsiveness. It is enabled by default and If I'd like to re-enable it I have to set :
context.Configuration.ProxyCreationEnabled = true;
context.Configuration.LazyLoadingEnabled = true;
用于急切加载它并不懒惰:它在我加载 dpc_gestion
时加载了所有导航属性.导航属性可以使用 include
方法加载.要启用此加载类型:
For the eager loading
It is not lazy: it loaded all navigation properties when I load dpc_gestion
. The navigation properties can be loaded using include
method. To enable this loading type :
context.Configuration.LazyLoadingEnabled = false;
用于显式加载这就像急切加载,但我们使用 Load
方法而不是 include
.
For the explicit loading
It is like the eager loading but we use Load
method instead of include
.
所以我想知道:
- 如果这个小简历是真的?
- 如果是真的,急切加载和显式加载有什么区别?
- 如果我使用延迟加载并调用例如
dpc_gestion.dpc_participant
,导航属性会加载吗?还是会出现异常? - 是否存在急切加载或显式加载在性能和响应能力方面优于延迟加载的情况?
- If this small resume is true ?
- If it is true, what is the difference between eager and explicit loading?
- If I Use lazy loading and I call for example
dpc_gestion.dpc_participant
, does the navigation properties loads?or I will get an exception? - Is there a case when eager loading or explicit loading were better than lazy loading in performance and responsiveness?
谢谢
推荐答案
如果这个小简历是真的?
If this small resume is true ?
是的.
如果是真的,急切加载和显式加载有什么区别?
If it is true, what is the difference between eager and explicit loading?
急切加载与延迟加载相反,但显式加载类似于延迟加载,除了:您在代码中显式检索相关数据;当您访问导航属性时,它不会自动发生.您可以通过获取实体的对象状态管理器条目并为集合调用 Collection.Load
方法或为包含单个属性的属性调用 Reference.Load
方法来手动加载相关数据实体.
Eager loading is the opposite of Lazy loading but Explicit loading is similar to lazy loading, except that: you explicitly retrieve the related data in code; it doesn't happen automatically when you access a navigation property. You load related data manually by getting the object state manager entry for an entity and calling the Collection.Load
method for collections or the Reference.Load
method for properties that hold a single entity.
来自 技术博客:
渴望加载:
急切加载是 与延迟加载相反的: 过程加载一组特定的相关对象以及对象查询中明确要求的.
Eager loading is the opposite of Lazy loading which is : The process of loading a specific set of related objects along with the objects that were explicitly requested in the query.
显式加载:
显式加载定义为:当查询返回对象时,相关对象不会同时加载.默认情况下,它们是在使用 Load 方法明确请求之前不加载导航属性.
Explicit loading is defined as : when objects are returned by a query, related objects are not loaded at the same time. By default, they are not loaded until explicitly requested using the Load method on a navigation property.
还有:
如果我使用延迟加载并调用例如 dpc_gestion.dpc_participant
,导航属性会加载吗?还是会出现异常?
If I Use lazy loading and I call for example
dpc_gestion.dpc_participant
, does the navigation properties loads?or I will get an exception?
您没有遇到任何异常,并且应该会加载导航属性.
You don't get any exception and the navigation properties should loads.
是否存在急切加载或显式加载更好的情况与延迟加载相比性能和响应能力?
Is there a case when eager loading or explicit loading were better than lazy loading in performance and responsiveness?
当您需要主表的所有检索行的相关数据时,
预加载通常更有效.而且当关系不是太多时,渴望加载将是减少服务器上进一步查询的好习惯.但是,当您知道您不会立即需要某个属性时,延迟加载 可能是一个不错的选择.在您的数据库上下文将被处理并且不再发生延迟加载的情况下,急切加载也是一个不错的选择.例如考虑以下内容:
Eager loading is typically more efficient when you need the related data for all retrieved rows of the primary table. And also when relations are not too much, eager loading will be good practice to reduce further queries on server. But when you know that you will not need a property instantly then lazy loading maybe a good choice. And also eager loading is a good choice in a situation where your db context would be disposed and lazy loading could not take place anymore. For example consider the following:
public List<Auction> GetAuctions()
{
using (DataContext db = new DataContext())
{
return db.Auctions.ToList();
}
}
调用该方法后,由于db
已被释放,无法懒加载相关实体,这里Eager Loading会是更好的选择.
After calling this method, You cannot load the related entity lazily because the db
is disposed and so the Eager Loading would be a better choice here.
还有一点需要注意的是:延迟加载会产生多个SQL请求,而急切加载会通过一个请求加载数据.Eager loading 也是解决 ORM 中 n+1 选择问题 的不错选择.看看这篇文章:什么是 n+1 选择问题?
One more thing to note is: Lazy loading will produce several SQL request while Eager loading load data with one request. Eager loading is also a good choice to solve the n+1 selects issue in ORMs. Have a look at this post: What is the n+1 selects issue?
这篇关于EF6 中的 Eager 、 Lazy 和显式加载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!