C# EF6中的渴望、懒惰和显式加载

C# EF6中的渴望、懒惰和显式加载,c#,entity-framework,orm,entity-framework-6,lazy-loading,C#,Entity Framework,Orm,Entity Framework 6,Lazy Loading,我读过这本书和这本书,但我不完全理解每种加载类型的用法 我解释一下 我有这个POCO: public partial class dpc_gestion { public dpc_gestion() { this.ass_reunion_participant = new HashSet<ass_reunion_participant>(); this.dpc_participant = new HashSet<dpc_parti

我读过这本书和这本书,但我不完全理解每种加载类型的用法

我解释一下

我有这个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; }
}
  • 用于紧急加载 它不是懒惰的:当我加载dpc\u-gestion时,它加载了所有导航属性。可以使用
    include
    方法加载导航属性。要启用此加载类型,请执行以下操作:

    context.Configuration.LazyLoadingEnabled = false;
    
  • 用于显式加载 这与急切加载类似,但我们使用
    Load
    方法而不是
    include

  • 所以我想知道:

  • 如果这个小简历是真的
  • 如果这是真的,那么即时加载和显式加载之间的区别是什么
  • 如果我使用延迟加载并调用例如
    dpc_gestion.dpc_participant
    ,是否加载导航属性?或者我将获得异常
  • 在性能和响应性方面,是否存在急切加载或显式加载优于惰性加载的情况

  • 谢谢

    问题1和问题2:

    您对延迟加载快速加载的解释是正确的。
    显式加载的使用与您描述的略有不同

    EntityFramework
    返回
    IQueryable
    对象,这些对象基本上包含对数据库的查询。但直到第一次枚举它们时,才会执行这些操作。
    Load
    执行查询,以便将其结果存储在本地。
    调用
    Load
    与调用
    ToList
    并丢弃
    List
    是一样的,没有创建
    列表的开销

    问题3:

    如果使用延迟加载,
    EntityFramework
    将负责为您加载导航属性,因此不会出现异常。
    请记住,这可能需要一段时间,并使您的应用程序没有响应

    问题4:

    在断开连接的情况下(例如网络应用程序),您不能使用延迟加载,因为这些对象被转换为DTO,然后不会被
    EntityFramework
    跟踪

    此外,如果您知道要使用导航属性,那么最好立即加载,这样您就不必等到从数据库加载了导航属性。
    例如,假设您将结果存储在列表中,并将其绑定到WPF数据网格。如果DataGrid访问一个尚未加载的属性,用户将经历一个明显的超时,直到显示该属性为止。此外,应用程序在加载期间不会响应(如果不异步加载)

    如果这个小简历是真的

    如果这是真的,那么即时加载和显式加载之间的区别是什么

    即时加载延迟加载相反,但是显式加载延迟加载类似,除了:您显式地在代码中检索相关数据;当您访问导航属性时,它不会自动发生。通过获取实体的对象状态管理器条目并调用集合的
    Collection.load
    方法或包含单个实体的属性的
    Reference.load
    方法,可以手动加载相关数据

    发件人:

    快速加载:

    急切加载是
    惰性加载相反的,后者是:过程 与对象一起加载一组特定的相关对象 在查询中显式请求的

    显式加载:

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

    以及:

    如果我使用延迟加载并调用例如
    dpc\u gestion.dpc\u participant
    ,是否加载导航属性?或者我将得到一个异常

    您不会得到任何异常,应该加载导航属性

    是否有一种情况下,即时加载或显式加载更好 在性能和响应性方面比延迟加载更好吗

    当您需要主表的所有检索行的相关数据时,快速加载通常更有效。而且,当关系不是太多时,急切加载将是减少服务器上进一步查询的良好实践。但是,当您知道您不会立即需要一个属性时,延迟加载可能是一个不错的选择。而且,在数据库上下文将被处理,惰性加载将不再发生的情况下,急切加载也是一个不错的选择。例如,考虑以下内容:

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

    public List

    在这里,您将学习如何在实体图中显式加载相关实体显式加载在EF 6和EF Core中均有效

    即使禁用了延迟加载(在EF 6中),仍然可以延迟加载相关实体,但必须通过显式调用完成。使用
    Load()
    方法显式加载相关实体。考虑下面的例子。

    using (var context = new SchoolContext())
    {
         var student = context.Students
                                  .Where(s => s.FirstName == "Bill")
                                 .FirstOrDefault<Student>();
    
         context.Entry(student).Reference(s => s.StudentAddress).Load(); 
         // loads StudentAddress
         context.Entry(student).Collection(s => s.StudentCourses).Load(); 
         // loads Courses collection      
    }
    

    在上面的示例中,
    .Collection(s=>s.StudentCourses).Query()
    允许我们为
    StudentCourses
    实体编写进一步的查询。

    @lamloumiaafif…需要注意的是:延迟加载将产生多个SQL请求,而急切地加载数据时只需一个请求,设置
    LazyLoadingEnabled=false
    不会启用即时加载。它禁用延迟加载。启用延迟加载时,可以使用
    Include
    立即加载。因此请记住,对于延迟加载,上下文必须处于活动状态
    using (var context = new SchoolContext())
    {
         var student = context.Students
                                  .Where(s => s.FirstName == "Bill")
                                 .FirstOrDefault<Student>();
    
         context.Entry(student).Reference(s => s.StudentAddress).Load(); 
         // loads StudentAddress
         context.Entry(student).Collection(s => s.StudentCourses).Load(); 
         // loads Courses collection      
    }
    
    using (var context = new SchoolContext())
    {
        var student = context.Students
                        .Where(s => s.FirstName == "Bill")
                        .FirstOrDefault<Student>();
    
        context.Entry(student)
                 .Collection(s => s.StudentCourses)
                   .Query()
                .Where(sc => sc.CourseName == "Maths")
                .FirstOrDefault();
    }