鉴于:
public SomeEntity Read(int primaryKey)
{
SomeEntity myEntity;
using (var context = new MyEntities2())
{
context.Configuration.LazyLoadingEnabled = false;//This line is wacky
myEntity = context.SomeEntities.SingleOrDefault(ct => ct.PrimaryKey == primaryKey);
if (myEntity == null)
return myEntity;
//Force eager Load...
var bypassDeferredExecution = myEntity.RelatedTable1.ToList();
var bypassDeferredExecution2 = myEntity.RelatedTable2.ToList();
}
return myEntity;
}
如果我设置LazyLoadingEnabled = false
然后myEntity.RelatedTable1.Count == 0。
保留默认值,LazyLoadingEnabled = true
然后 myEntity.RelatedTable1.Count == 2。
我的理解是延迟加载和急切加载是截然相反的。我强迫急切加载。无论我是否使用延迟加载,我都希望我的相关表(交叉引用表)有 2 个结果。所以在我看来,这些结果毫无意义。
为什么延迟加载会影响我的结果?