使用SelectMany时,我似乎无法弄清楚如何使EF Core包含/加载相关对象。
I cant seem to figure out how to get EF Core to include / load related objects when using SelectMany.
context.MyObject .Where(w => w.Id == Id) .SelectMany(m => m.SubObject) .Include(i => i.AnotherType)本来以为上面的方法会起作用,但是折叠的SubObject集合的AnotherObject为null而不是
Would have thought something like the above would work, however the collapsed SubObject collection has the AnotherObject being null and not included.
寻找时间。
任何帮助将不胜感激。
谢谢
推荐答案
本来会以为类似上面的方法会起作用
Would have thought something like the above would work
它曾经在EF6中工作,但目前不受EF Core支持-它允许您仅使用渴望加载的实体如加载Re相关数据-文档的被忽略的包括部分:
It used to work in EF6, but currently is not supported by EF Core - it allows you to use eager load only the entity which the query starts with, as mentioned in the Loading Related Data - Ignored Includes section of the documentation:
如果您更改查询以使它不再返回查询开始的实体类型的实例,然后将忽略包含运算符。
If you change the query so that it no longer returns instances of the entity type that the query began with, then the include operators are ignored.
因此,我们渴望在您的方案中加载工作时,查询应该是这样的(假设您在 SubObject 中具有反向导航或FK属性):
So to get the eager loading work in your scenario, the query should be like this (assuming you have inverse navigation or FK property in SubObject):
context.SubObject .Where(so => so.Object.Id == Id) // or so.ObjectId == Id .Include(i => i.AnotherType)更多推荐
实体框架核心SelectMany然后包含
发布评论