IQueryable的< T> .INCLUDE()被忽略 [英] IQueryable<T>.Include() gets ignored

查看:118
本文介绍了IQueryable的< T> .INCLUDE()被忽略的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有孩子之间的相互关系为1至M.我需要查​​询孩子的共同实体用在一个SQL查询的父母,但包含方法不正确工作的一些情况。
这人让两个家长正确的单个查询孩子表(通过JOIN):

I have Parent and Child entities related to each other as 1 to M. I need to query childs together with parents within a single SQL query, but the Include method is not properly working for some cases.
This one makes a correct single query for both Parent and Child tables (via JOIN):

var r1 =
    ctx.Set<Parent>()
       .Include(p => p.Childs)
       .Where(p => p.Id == 1)
       .ToList();

在我即时创建一个匿名对象,孩子们迷路和SQL只包含父母的领域。儿童的检索依然懒惰 - 他们仍然没有加载:

Once i create an anonymous object on the fly, children are getting lost and SQL contains only Parent's fields. Retrieval of children remains lazy - they are still not loaded:

var r2 =
    ctx.Set<Parent>()
       .Include(p => p.Childs)
       .Where(p => p.Id == 2)
       .Select(p => new { myParent = p})
       .ToList();

问题:

  1. 为什么如此?
  2. 如何我可以构造一个新的匿名对象,在我的LINQ所以孩子的不是歌厅失去了什么?

P.S。我想保留父母的童车财产是虚拟的。

p.s. i'd like keep Childs property of Parent virtual.

推荐答案

这是在EF已知我所有版本的一个普遍问题。 EF力图通过'包括'尽可能,但是当的查询的形状改变中,'包括'不可逆地丢失。

This is a general problem in all versions of EF known to me. EF tries hard to pass the 'includes' as far as possible, but when "the shape of the query changes", the 'includes' are irreversibly lost.

。例如查询变化的形,当:

The "shape" of the query changes for example, when:

  • 在投影时(选择不是整个对象,但只是一些领域,或不同的对象)
  • 在一组,或由其他聚合使用
  • ..并可能在一些情况下,目前我不记得了。

可悲的是,我也是不记得MSDN上,我偶然发现的解释的查询形状。如果我发现了,我会在这里掉落的链接。

Sadly, I also dont remember where on MSDN I stumbled upon the "shape of the query" explanation. If I find it, I'll drop here a link.

解决的办法其实很简单:只需指定包括部分不早,但最终的结果。因此,而不是 set.include(X)的开始,做。选择(.. =&gt;新建{...,X})以包括X手动。它还分组时的作品,你可以做一个预测有太多。

The solution is actually quite simple: simply specify the 'include' part not early, but at the final result. So, instead of set.include(x) at beginning, do .Select( .. => new { .., x }) to include the 'x' manually. It also works during grouping, as you can do a projection there too.

然而,这不是一个解决方案。这是一个手动补丁/修补程序,不解决任何问题。考虑到你可能要暴露的IQueryable&LT;>通过一些接口,你可能想揭露了基本查询有些事情已经.Included。当然,这仅仅是不可能做在一般的方式,仿佛接口的客户端执行的投影或分组时,他就失去了包括他甚至不知道哪些应该是。对我来说,这是EF一个重大缺陷。

However, this is not a solution. This is a manual patch/hotfix, which does not solve anything. Considering that you might want to expose a "IQueryable<>" through some interface, you may like to expose a "base query" with some things already .Included. And of course, this is simply not possible to do in a general way, as if the client of the interface does a projection or grouping, he will lose the includes and he will not even know which ones should be. For me, this is a major deficiency in EF.

编辑:刚刚发现了一个:<一href="http://stackoverflow.com/questions/14232481/include-in-following-query-does-not-include-really">.Include在下面的查询不包括真正的不是MSDN,但一样好。

just found one: .Include in following query does not include really Not MSDN, but just as good.

这篇关于IQueryable的&LT; T&GT; .INCLUDE()被忽略的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆