实体框架核心SelectMany然后包含

编程入门 行业动态 更新时间:2024-10-24 14:24:55
本文介绍了实体框架核心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集合的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然后包含

本文发布于:2023-11-10 22:40:48,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1576664.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:实体   框架   核心   SelectMany

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!