gpt4 book ai didi

c# - Entity Framework Core SelectMany 然后包含

转载 作者:太空狗 更新时间:2023-10-29 22:57:04 26 4
gpt4 key购买 nike

我似乎无法弄清楚如何让 EF Core 在使用 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

它曾经在 EF6 中工作,但目前不受 EF Core 支持 - 它允许您仅使用预加载查询开始的实体,如 Loading Related Data 中所述- 忽略包含文档部分:

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 属性):

context.SubObject
.Where(so => so.Object.Id == Id) // or so.ObjectId == Id
.Include(i => i.AnotherType)

关于c# - Entity Framework Core SelectMany 然后包含,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44519325/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com