C# DBContext lazyloadingenabled 设置为 true 仍然默认加载相关实体
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10644590/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
DBContext lazyloadingenabled set to true still loads related entities by default
提问by bretcj7
LazyLoadingEnabled is specifically set to true to prevent the related entities from loading in the context I'm using.
LazyLoadingEnabled 专门设置为 true 以防止在我使用的上下文中加载相关实体。
A drug class has a list of drugidentity objects in it.
药物类中有一个药物身份对象列表。
public class Drug
{
public virtual List<DrugIdentity> DrugIdentities { get; set; }
}
A specific configuration for the class sets the key and hasmany relationship if I wanted to include the related entity to be loaded.
如果我想包含要加载的相关实体,则该类的特定配置会设置 key 和 hasmany 关系。
public DrugConfiguration()
{
this.HasKey(d => d.DrugID);
this.HasMany(d => d.DrugIdentities).WithOptional(d => d.Drug).Map(d => d.MapKey("DrugID"));
}
When the Drug context is loaded using a linq query the object shows it contains related DrugIdentities when it shouldn't.
当使用 linq 查询加载 Drug 上下文时,该对象显示它包含相关的 DrugIdentities,而它不应该包含相关的 DrugIdentities。
context.Configuration.LazyLoadingEnabled = true;
var drugs = from d in context.Drug
where d.Active == true
select d;
drugs[0].DrugIdentities Count = 1
药物 [0].药物身份计数 = 1
I would expect drugs[0].DrugIdentities to equal NULL since lazyloading was set to true?
由于延迟加载设置为 true,我希望药物 [0].DrugIdentities 等于 NULL?
回答by tdykstra
To disable lazy loading, set LazyLoadingEnabled to false rather than true. See Lazy, Eager, and Explicit Loading of Related Datain
要禁用延迟加载,请将 LazyLoadingEnabled 设置为 false 而不是 true。参见Lazy, Eager, and Explicit Loading of Related Datain
回答by bretcj7
You have to specifically set ProxyCreationEnabled = falseif you want to set LazyLoadingEnabled = true.
ProxyCreationEnabled = false如果你想设置,你必须专门设置LazyLoadingEnabled = true。
The test passed on what I expected. The first query returns the Drugsobject and NULLfor DrugEntities. Second query returns the DrugEntitiessince I used the Includeto do the eager loading.
测试通过了我的预期。第一个查询返回Drugs对象和NULLfor DrugEntities。第二个查询返回 ,DrugEntities因为我使用Include来进行急切加载。
var queryDrug = from d in context.Drug
where d.Active == true
select d;
var drugresults = from d in context.Drug.Include(e => e.DrugIdentities)
where d.Active == true
select d;
回答by Roy Liu
It is the very behavior of lazy loading. if you just use the property of drugs, then there will not be any sql to query DrugIdentities. if you use DrugIdentities even just watch it in debug window, then there will be sql to query DrugIdentities and each drug with a single DrugIdentities search query. You can prove the behavior through peeking the sql captured by SQL Profiler. If you want DrugIdentities to be null while querying drug, you may change the model by remove virtual key word before DrugIdentities. Then when you query drug, DrugIdentities will keep null until you load DrugIdentities to context by another query.
这正是延迟加载的行为。如果只是使用drug的属性,那么就不会有查询DrugIdentities的sql了。如果您使用 DrugIdentities 甚至只是在调试窗口中观看它,那么将有 sql 查询 DrugIdentities 和每个药物,并使用单个 DrugIdentities 搜索查询。您可以通过查看 SQL Profiler 捕获的 sql 来证明该行为。如果您希望在查询药物时 DrugIdentities 为空,您可以通过删除 DrugIdentities 之前的虚拟关键字来更改模型。然后,当您查询药物时, DrugIdentities 将保持为空,直到您通过另一个查询将 DrugIdentities 加载到上下文中。

