实体框架中的导航属性问题
当我执行此查询时,我可以在 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;
这是为什么?
When I execute this query I can navigate in TypeP
property:
var items = from item in context.ProductosBodegas.Include("Product.TypeP")
select item;
But when I execute this query the TypeP
Property is null
:
var items = from item in context.ProductosBodegas.Include("Product.TypeP")
select item.Product;
Why is this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
据我所知,只要您不想返回完整实体而只想返回投影,实体框架就会忽略包含。例如,请参阅答案 此处。我不确定这是否仍然适用于所有类型的预测,但显然它在您的情况下仍然有效。
您可以通过添加要加载到匿名类型中的导航属性来解决此问题:(
此处不再需要
.Ininclude
。)执行此查询后(通过使用.ToList ()
例如)您可以仅投影到您想要拥有的匿名类型的部分,如下所示:此
products
集合中的元素已加载TypeP现在参考属性。
编辑:
重要提示:不要更改
.ToList
和.Select...
的顺序。虽然这……在语法上也是正确的,并且还返回产品的枚举,但在这种情况下将不会加载
TypeP
引用。必须首先在数据库中执行对匿名类型的查询,并将匿名类型集合加载到内存中。然后你可以通过.Select
方法丢弃你不想要的匿名类型部分。As far as I know Entity Framework ignores Includes as soon as you don't want to return full entities but only projections. See for instance the answer here. I am not sure if that is still valid for all types of projections but apparently it is still valid in your situation.
You can workaround this by adding the navigation property you want to have loaded into an anonymous type:
(You don't need
.Include
here anymore.) After executing this query (by using.ToList()
for instance) you can project to only the part of the anonymous type you want to have, like so:The elements in this
products
collection have loaded theTypeP
reference property now.Edit:
Important note: Don't change the order of
.ToList
and.Select...
. While this ...... is also syntactically correct and also returns an enumeration of products, the
TypeP
reference will NOT be loaded in this case. The query for the anonymous type must be executed at first in the database and the anoynmous type collection loaded into memory. Then you can throw away the part of the anoynmous type you don't want to have by the.Select
method.你应该先加载产品
you should load product first
看起来包含仅影响直接返回的对象:
http://msdn.microsoft.com /en-us/library/bb896272.aspx
否则你可以调用
但是如果在循环中使用,这可能(并且将会)导致性能问题(N+1 select)。
另一种选择是“反转”您的查询,假设 Product 和 ProductosBodegas 之间的关系是双向的:
Looks like Include only affects the directly returned object :
http://msdn.microsoft.com/en-us/library/bb896272.aspx
Otherwise you can call
But this can (and will) lead to perfornance issues (N+1 select), if used in a loop.
Another option would be to "reverse" your query, assuming relationship between Product and ProductosBodegas is bidirectional :