gpt4 book ai didi

c# - EF6 中的急切、延迟和显式加载

转载 作者:IT王子 更新时间:2023-10-29 04:27:29 24 4
gpt4 key购买 nike

我读过这个tutorial还有这个article但我不完全了解每种加载类型的用途。

我来解释

我有这个 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 所有导航属性不会加载。这种类型的加载在性能和响应能力方面是最好的。它默认启用,如果我想重新启用它,我必须设置:

    context.Configuration.ProxyCreationEnabled = true;    
    context.Configuration.LazyLoadingEnabled = true;
  2. 用于预加载它并不懒惰:它在我加载 dpc_gestion 时加载了所有导航属性。可以使用 include 方法加载导航属性。要启用此加载类型:

    context.Configuration.LazyLoadingEnabled = false;
  3. 对于显式加载它类似于预加载,但我们使用 Load 方法而不是 include

所以我想知道:

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

谢谢

最佳答案

If this small resume is true ?

是的。

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

Eager loadingLazy loading相反,但Explicit loading类似于lazy loading除此之外:您在代码中明确检索相关数据;当您访问导航属性时,它不会自动发生。您可以通过获取实体的对象状态管理器条目并为集合调用 Collection.Load 方法或为包含单个属性的属性调用 Reference.Load 方法来手动加载相关数据实体。

来自 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.

和:

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?

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

预先加载 通常效率更高。而且当关系不是太多时,eager loading 将是减少服务器上进一步查询的好习惯。但是当您知道您不会立即需要某个属性时,延迟加载 可能是一个不错的选择。在您的数据库上下文将被处理并且延迟加载不再发生的情况下,预加载也是一个不错的选择。例如考虑以下内容:

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

调用此方法后,您将无法延迟加载相关实体,因为 db 已被释放,因此Eager Loading 将是更好的选择。

另外需要注意的是:Lazy loading会产生多个SQL请求,而Eager loading会产生一个请求加载数据。 Eager loading 也是解决 ORM 中的 n+1 选择问题的不错选择。看看这篇文章:What is the n+1 selects issue?

关于c# - EF6 中的急切、延迟和显式加载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34627865/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com