渴望,懒惰和明确加载EF6 [英] Eager , Lazy and explicit loading in EF6

查看:114
本文介绍了渴望,懒惰和明确加载EF6的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经阅读了这个教程,这个文章,但我不明白每种加载类型的使用。



我解释



我有这个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;组; }
public Nullable< int> dpc_id_gdp_fk {get;组; }
public Nullable< int> dpc_id_theme {get;组; }
public int dpc_id_animateur_fk {get;组; }
public Nullable< System.DateTime> dpc_date_creation {get;组; }
public Nullable< System.DateTime> dpc_date_fin {get;组; }
public Nullable< System.DateTime> dpc_date_engag_anim {get;组; }
public Nullable< bool> dpc_flg_let_engag_anim {get;组; }
public Nullable< bool> dpc_flg_fsoins_anim {get;组; }
public virtual ICollection< ass_reunion_participant> ass_reunion_participant {get;组; }
public virtual theme_dpc theme_dpc {get;组; }
public virtual gdp_groupe_de_pair gdp_groupe_de_pair {get;组; }
public virtual ICollection< dpc_participant> dpc_participant {get;组; }
public virtual ICollection< dpc_reunion> dpc_reunion {get;组;
}

我明白了:


  1. 对于Lazy加载:因为加载是懒惰的,如果我调用dbset dpc_gestion 所有导航属性将不会加载。这种类型的加载是性能和响应能力最好的。它默认启用,如果我想重新启用它,我必须设置:

      context.Configuration.ProxyCreationEnabled = true; 
    context.Configuration.LazyLoadingEnabled = true;


  2. 渴望加载
    它不是懒惰:加载所有导航属性加载 dpc_gestion 。可以使用 include 方法加载导航属性。要启用此加载类型:

      context.Configuration.LazyLoadingEnabled = false; 


  3. 显式加载
    它就像我们使用加载方法而不是包含


所以我想知道:


  1. 如果这个小简历是真的吗?

  2. 如果是真的,渴望和显式加载有什么区别?

  3. 如果我使用延迟加载并且我调用例如 dpc_gestion.dpc_participant ,导航属性是否加载?或者我会得到例外?

  4. 是否有如果您希望加载或显式加载比在延迟性能和响应速度方面的延迟加载更好?

谢谢

解决方案


如果这个小简历是真的?


是的。


如果是真的,渴望和显式加载有什么区别? p>

渴望加载延迟加载相反,但显式加载类似于延迟加载,除了:您明确地检索代码中的相关数据;当您访问导航属性时,它不会自动发生。通过获取实体的对象状态管理器条目并为集合调用 Collection.Load 方法或 Reference.Load 方法,用于持有单个实体的属性。



techblog


渴望载入中:



渴望加载是 :进程
加载特定的一组相关对象以及查询中明确请求的对象



显式加载:



显式加载定义为:当查询返回对象时,
相关对象不会同时加载。默认情况下,它们是
,直到明确请求使用
导航属性上的Load方法。


和:


如果我使用延迟加载,我调用例如 dpc_gestion.dpc_participant ,导航属性是否加载?或者我会得到一个例外?


你没有得到任何异常,导航属性应该加载。


在性能和响应速度方面,急切加载或显式加载更好的
是否比延迟加载的情况? >

当您需要主表的所有已检索行的相关数据时,希望加载通常会更有效。而且当关系不是太多时,渴望加载将是减少服务器上进一步查询的好习惯。但是当你知道你不需要一个财产,那么懒惰加载也许是一个不错的选择。而且,在您的数据库环境将被处理并且延迟加载不再发生的情况下,也希望加载是一个很好的选择。例如考虑以下内容:

  public List< Auction> GetAuctions()
{
using(DataContext db = new DataContext())
{
return db.Auctions.ToList();
}
}

调用此方法后,无法加载相关实体懒洋洋地,因为 db 被处理,所以渴望加载将是一个更好的选择。



另外需要注意的是:延迟加载将产生几个SQL请求,而加载加载数据时需要一个请求。 渴望加载也是解决ORM中 n + 1选择问题的不错选择。
看看这篇文章:什么是n + 1选择问题?


I have read this tutorial and this article but I don't understand exactly the use of each loading type.

I Explain

I have this 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; }
}

I understood this :

  1. 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;
    

  2. 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;
    

  3. For the explicit loading It is like the eager loading but we use Load method instead of include.

So I'd like to know :

  1. If this small resume is true ?
  2. If it is true, what is the difference between eager and explicit loading?
  3. 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?
  4. Is there a case when eager loading or explicit loading were better than lazy loading in performance and responsiveness?

Thanks

解决方案

If this small resume is true ?

Yes.

If it is true, what is the difference between eager and explicit loading?

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.

From techblog:

Eager Loading :

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.

Explicit Loading :

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.

And:

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();
    }
}

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.

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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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