且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

IQueryable< T> .Include()被忽略

更新时间:2022-03-13 15:02:35

这是所有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.

形状的查询更改例如:


  • 使用投影(选择不是整个对象,只是一些字段,或不同的对象)

  • 使用group-by或其他聚合

  • ..可能还有更多的情况,目前我不记得了。 >
  • a projection is used (select not whole object but just some fields, or different object)
  • a group-by or other aggregation is used
  • .. and probably in some more cases, which currently I dont remember.

令人遗憾的是,我也不记得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.

解决方案其实很简单:只是简单地指定'include'部分,但最终结果。所以,而不是开始时 set.include(x),请执行 .Select(.. => new {..,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,您可能希望公开一些已经包含的基本查询。当然,这种做法一般都不可能,就像接口的客户端做一个投影或分组一样,他会失去包含,他甚至不知道哪个应该是是。对我来说,这是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.

编辑:刚找到一个:。包含在以下查询中不包括真的不是MSDN,但是一样好。

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