简体   繁体   English

实体框架中导航属性的问题

[英]problem with navigation properties in entity framework

When I execute this query I can navigate in TypeP property: 当我执行此查询时,我可以在TypeP属性中导航:

var items = from item in context.ProductosBodegas.Include("Product.TypeP")
            select item;

But when I execute this query the TypeP Property is null : 但是当我执行此查询时, TypeP属性为null

var items = from item in context.ProductosBodegas.Include("Product.TypeP")
            select item.Product;

Why is this? 为什么是这样?

Looks like Include only affects the directly returned object : 看起来Include只会影响直接返回的对象:

http://msdn.microsoft.com/en-us/library/bb896272.aspx http://msdn.microsoft.com/en-us/library/bb896272.aspx

Otherwise you can call 否则你可以打电话

item.TypePReference.Load()

But this can (and will) lead to perfornance issues (N+1 select), if used in a loop. 但如果在循环中使用,这可能(并且将会)导致性能问题(N + 1选择)。

Another option would be to "reverse" your query, assuming relationship between Product and ProductosBodegas is bidirectional : 假设Product和ProductosBodegas之间的关系是双向的,那么另一种选择是“反转”您的查询:

var items = context.Products
    .Include("TypeP")
    .Where(p => p.ProductosBodegas.Any( /* include condition here if needed */ ))

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: 您可以通过将要加载的导航属性添加到匿名类型来解决此问题:

var items = from item in context.ProductosBodegas
            select new {
                Product = item.Product,
                TypeP = item.Product.TypeP
            };

(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: (你不需要.Include here。)执行这个查询后(例如使用.ToList() )你可以只投射到你想拥有的匿名类型的部分,如下所示:

var products = items.ToList().Select(x => x.Product);

The elements in this products collection have loaded the TypeP reference property now. products集合中的元素现在已加载TypeP引用属性。

Edit: 编辑:

Important note: Don't change the order of .ToList and .Select... . 重要说明:请勿更改.ToList.Select...的顺序。 While this ... 虽然这......

var products = items.Select(x => x.Product).ToList();

... is also syntactically correct and also returns an enumeration of products, the TypeP reference will NOT be loaded in this case. ...在语法上也是正确的,并且还返回产品的枚举,在这种情况下不会加载TypeP引用。 The query for the anonymous type must be executed at first in the database and the anoynmous type collection loaded into memory. 必须首先在数据库中执行匿名类型的查询,并将anoynmous类型集合加载到内存中。 Then you can throw away the part of the anoynmous type you don't want to have by the .Select method. 然后你可以通过.Select方法丢弃你不想拥有的anoynmous类型的部分。

you should load product first 你应该先加载产品

var items = from item in context.ProductosBodegas.Include("Product").Include("Product.TypeP")
            select item;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM