当我执行此查询时,我可以在 TypeP
属性中导航:
var items = from item in context.ProductosBodegas.Include("Product.TypeP")
select item;
但是当我执行此查询时,TypeP
属性为 null
:
var items = from item in context.ProductosBodegas.Include("Product.TypeP")
select item.Product;
这是为什么?
最佳答案
看起来 Include 只影响直接返回的对象:
http://msdn.microsoft.com/en-us/library/bb896272.aspx
否则你可以调用
item.TypePReference.Load()
但是如果在循环中使用,这可能(并且将会)导致性能问题(N+1 选择)。
假设 Product 和 ProductosBodegas 之间的关系是双向的,另一种选择是“反转”您的查询:
var items = context.Products
.Include("TypeP")
.Where(p => p.ProductosBodegas.Any( /* include condition here if needed */ ))
关于c# - Entity Framework 中的导航属性问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5500657/