缺省情况下,将DBContext lazyloadingenabled设置为true仍会加载相关实体 [英] DBContext lazyloadingenabled set to true still loads related entities by default
问题描述
LazyLoadingEnabled专门设置为true,以防止相关实体在我正在使用的上下文中加载.
LazyLoadingEnabled is specifically set to true to prevent the related entities from loading in the context I'm using.
毒品类别中有一个毒品身份对象列表.
A drug class has a list of drugidentity objects in it.
public class Drug
{
public virtual List<DrugIdentity> DrugIdentities { get; set; }
}
如果要包含要加载的相关实体,则该类的特定配置会设置键和hasmany关系.
A specific configuration for the class sets the key and hasmany relationship if I wanted to include the related entity to be loaded.
public DrugConfiguration()
{
this.HasKey(d => d.DrugID);
this.HasMany(d => d.DrugIdentities).WithOptional(d => d.Drug).Map(d => d.MapKey("DrugID"));
}
使用linq查询加载Drug上下文时,该对象显示该对象在不应该包含相关的DrugIdentities的情况下.
When the Drug context is loaded using a linq query the object shows it contains related DrugIdentities when it shouldn't.
context.Configuration.LazyLoadingEnabled = true;
var drugs = from d in context.Drug
where d.Active == true
select d;
drugs [0] .DrugIdentities Count = 1
drugs[0].DrugIdentities Count = 1
我希望药物[0].由于延迟加载设置为true,DrugIdentities是否等于NULL?
I would expect drugs[0].DrugIdentities to equal NULL since lazyloading was set to true?
推荐答案
要禁用延迟加载,请将LazyLoadingEnabled设置为false而不是true.参见
To disable lazy loading, set LazyLoadingEnabled to false rather than true. See Lazy, Eager, and Explicit Loading of Related Data in