Entity Framework Core SelectMany 然后包含 [英] Entity Framework Core SelectMany then Include

查看:25
本文介绍了Entity Framework Core SelectMany 然后包含的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我似乎无法弄清楚在使用 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 集合的另一个对象是 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 支持 - 它允许您仅使用预加载查询开始的实体,如 加载相关数据 - 文档的忽略的包含部分:

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)

这篇关于Entity Framework Core SelectMany 然后包含的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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