[英]Eager loading and navigation properties
var ret = (from f in context.foo
join b in context.bar on f.barid = b.barid
select f).ToList();
my returning list cointains all foos that have a barId, it also contains all navigation properties. 我的返回列表包含所有具有barId的foo,它还包含所有导航属性。 What I mean by that is, 我的意思是,
context.foo.mark
is populated even though I didnt not explicitly include it nor did i access it during the query. 即使我没有显式包括它,也没有在查询期间访问它,但context.foo.mark
填充context.foo.mark
。 I have lazy loading turned on, why is this occuring? 我启用了延迟加载,为什么会这样?
To elaborate on my question, somehow my related entities are getting loaded from the above query.I am curious as to how that is occurring, I have lazy loading enabled and I am not accessing any of the related objects 为了详细说明我的问题,我从上面的查询中以某种方式获取了我的相关实体。我很好奇这是如何发生的,我启用了延迟加载并且我没有访问任何相关的对象
The lazy loading inspection is kind of a "catch-22" type problem. 延迟加载检查是一种“ catch-22”类型的问题。 With Lazy Loading turned on, even a call to the property from the debugger will load the results as long as your context is still hanging around. 启用“延迟加载”后,只要上下文仍然存在,即使从调试器调用该属性也将加载结果。 Furthermore, if your context is still open from other queries, EF will maintain the state of those objects automatically and include them. 此外,如果您的上下文仍未通过其他查询打开,则EF将自动维护那些对象的状态并将其包括在内。
The only real way I can think of to determine if it is being lazily loaded or not is to inspect the SQL code sent to your database. 我能想到的唯一真正确定延迟加载的方法是检查发送到数据库的SQL代码。
First, add this line to your DbContext constructor: 首先,将此行添加到您的DbContext构造函数中:
this.Database.Log = s => System.Diagnostics.Debug.WriteLine(s); //remove in production
Then, run your code as normal (but don't stop in the debugger to inspect your object). 然后,正常运行您的代码(但不要在调试器中停止检查对象)。 Look at your debug console and inspect the SQL calls made. 查看您的调试控制台并检查进行的SQL调用。 My bet is that the SQL will not include the related properties. 我敢打赌,SQL将不包含相关属性。
If you run the code again, and stop the debugger to inspect the object properties, you should see another SQL call in the debug console fetching the related entities. 如果再次运行代码,并停止调试器以检查对象属性,则应该在调试控制台中看到另一个SQL调用,该调用正在提取相关实体。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.