- r - 以节省内存的方式增长 data.frame
- ruby-on-rails - ruby/ruby on rails 内存泄漏检测
- android - 无法解析导入android.support.v7.app
- UNIX 域套接字与共享内存(映射文件)
我读过这个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; }
}
我是这么理解的:
延迟加载:因为加载是延迟的,如果我调用 dbset dpc_gestion
所有导航属性不会加载。这种类型的加载在性能和响应能力方面是最好的。它默认启用,如果我想重新启用它,我必须设置:
context.Configuration.ProxyCreationEnabled = true;
context.Configuration.LazyLoadingEnabled = true;
用于预加载它并不懒惰:它在我加载 dpc_gestion
时加载了所有导航属性。可以使用 include
方法加载导航属性。要启用此加载类型:
context.Configuration.LazyLoadingEnabled = false;
对于显式加载它类似于预加载,但我们使用 Load
方法而不是 include
。
所以我想知道:
dpc_gestion.dpc_participant
,导航属性会加载吗?还是会出现异常?谢谢
最佳答案
If this small resume is true ?
是的。
If it is true, what is the difference between eager and explicit loading?
Eager loading与Lazy 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/
EF POCO 和 EF Code First 有什么区别? 如果我一开始只使用 POCO,我可以将 EF 与它们一起使用吗? 最佳答案 如果您首先使用 EF 代码,您将拥有 POCO 对象,并且数据
EF POCO 和 EF Code First 有什么区别? 如果我一开始只使用 POCO,我可以将 EF 与它们一起使用吗? 最佳答案 如果您首先使用 EF 代码,您将拥有 POCO 对象,并且数据
我有一个基于 .NET 4.8 和 EF 6.4.4 的项目。我们正在逐步迁移到 .Net Core,但在此过程中我可以创建一个 .NET Core 数据上下文类 EF Core 并将两者指向相同的实
我有以下 Entity Framework 5 代码第一类 public class Airplane { public int Id { get; set; } public int
我正在尝试使用 Entity Framework Core 对现有数据库进行逆向工程.我试着按照指示from Microsoft但我遇到了错误: Unable to find provider ass
当数据库不是由 EF 代码首先创建时,有没有办法检查 DbContext 是否与数据库匹配? 我正在寻找与 Database.CompatibleWithModel 类似的功能但没有元数据。 最佳答案
目前,我正在重构我的上下文方法的测试,以不再需要真正的数据库。我使用 Ef Core。 所以我通读了 Microsoft 文档如何测试上下文方法。我首先找到了 EF6 测试的文档,然后阅读了 EfCo
我正在使用 EF 6 Database.SqlQuery 语句来执行存储过程,并实现错误和事务处理,打开和关闭事务(处理多个记录,如果一个记录有错误,仅回滚此,并提交其他任何内容无错误完成)。 Lis
我首先使用 EF 数据库,因为我喜欢在 SQL Management Studio 中设计我的数据库,坦率地说,让 Visual Studio 直接从数据库创建所有实体非常容易,而无需执行任何代码。
我的项目中有几个迁移文件,由于我对上次迁移进行了手动修改,所以我不想使用“程序包管理器控制台”重新生成它。我只需要添加 1 列。所以在之前的迁移中手动添加了这个(我可以这样做,因为还没有人升级过)。
原文:https://bit.ly/2umidlb 作者:jon p smith 翻译:王亮 声明:我翻译技术文章不是逐句翻译的,而是根据我自己的理解来表述的。其中可能会去除一些本人实在不知道如何组
我们想开始一个新项目,我们决定从一开始就在一些表中使用 Columnstore 索引和聚簇索引,我们如何使用 Code First EF Core 3.1 做到这一点? 最佳答案 您应该将主键更改为非
我在 Entity Framework 6.0 上。这是一个开发问题,而不是生产问题。 我想我有一个相互矛盾的策略。 目前,我设置了 DropCreateDatabaseIfModelChanges
我在 VS 2012 RTM 上,使用 EF 5。我在做代码优先,但由于我只是在开发中,所以试图忽略代码迁移。为了避免它们,我有这一套 Database.SetInitializer(new Drop
我有复杂的审计字段类型 我的复杂类型: [ComplexType] public class AuditData { [Column("CreatorUserId")] public
我已经阅读了很多关于如何在关系中指定外键名称的帖子,没有遇到任何问题。不过,我想知道的是,有没有办法更改主键和关系的命名方式? 例如,您有一个以 UserId 作为主键的 User 表。 EF 代码第
我刚刚安装了新的Entity Framework 4.1 NuGet包,因此根据NuGet指令和this article of Scott Hanselman替换了EFCodeFirst包。 现在,想
我的应用程序基于 .NET 4.0 和 EF 4。我现在正在考虑升级到最新版本。 是否有任何可能对我的申请产生不利影响的重大变化或行为差异? 升级路径有多简单?升级到 EF 5 是否需要任何代码更改或
假设您必须使用 EF Code First 和 POCO 类开发支持多种语言的网站,您将如何对 POCO 类进行建模以支持这种情况? 通过多语言支持,我的意思不仅是拥有例如在一些资源文件中为您的 UI
我首先使用 EF 4.1 代码。鉴于以下类片段: public class Doctor { public virtual ICollection Hospitals { get; set;
我是一名优秀的程序员,十分优秀!