EF6 中的 Eager、Lazy 和显式加载 [英] Eager , Lazy and explicit loading in EF6

查看:21
本文介绍了EF6 中的 Eager、Lazy 和显式加载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已阅读此教程和此文章 但我不完全了解每种加载类型的用途.

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

我解释

我有这个 POCO :

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

我明白了:

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

  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;

  • 对于急切加载它并不懒惰:它在我加载 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.

    所以我想知道:

    1. 这个小简历是否属实?
    2. 如果是真的,那么急切加载和显式加载有什么区别?
    3. 如果我使用延迟加载并调用例如dpc_gestion.dpc_participant,导航属性是否加载?还是会出现异常?
    4. 是否存在急切加载或显式加载在性能和响应能力方面优于延迟加载的情况?
    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?

    谢谢

    推荐答案

    这个小简历是否属实?

    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?

    当您需要主表的所有检索行的相关数据时,

    快速加载通常更有效.而且当关系不是太多时,急切加载将是减少服务器上进一步查询的好习惯.但是,当您知道您不会立即需要某个属性时,延迟加载 可能是一个不错的选择.而且,在您的 db 上下文将被处理并且无法再进行延迟加载的情况下,预先加载也是一个不错的选择.例如考虑以下内容:

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

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