简体   繁体   English

Entity Framework Core Include/ThenInclude 获取数据

[英]Entity Framework Core Include/ThenInclude to get the data

I have tables in my database with the following one-to-many relations:我的数据库中有以下一对多关系的表:

a -> b
b -> c
b -> d

which are mapped to corresponding navigational properties in the model.它们映射到 model 中的相应导航属性。

I want to get data from "a" with all related data from "b, c, d".我想从“a”获取数据以及“b,c,d”的所有相关数据。

If I try:如果我尝试:

dbcontext.a.Include(x => x.b)
           .ThenInclude(x => x.c)
           .ThenInclude(x => x.d)

I get an error because this implies a relationship c -> d (instead of b -> d ).我收到一个错误,因为这意味着关系c -> d (而不是b -> d )。

Then, if I try:然后,如果我尝试:

dbcontext.a.Include(x => x.b)
           .ThenInclude(x => x.c)
           .Include(x => x.d)

I get an error because this implies a relationship a -> d (instead of b -> d ).我收到一个错误,因为这意味着关系a -> d (而不是b -> d )。

I'd appreciate help on how to correctly chain the methods.我很感激有关如何正确链接方法的帮助。

Try this if you use net5如果你使用 net5 试试这个

dbcontext.a.Include(x => x.b)
            .ThenInclude(x => x.c)
             .Include(x => x.b)
            .ThenInclude(x => x.d)

Or to avoid repeating you can try ( was tested by @Marko )或者为了避免重复,您可以尝试(由@Marko 测试)

dbcontext.a.Include(x => x.b)
            .ThenInclude(x => x.c)
             .Include(x => x.b.d)
           

but I prefer to use this syntax (fits for all versions)但我更喜欢使用这种语法(适用于所有版本)

dbcontext.a.Include(x => x.b)
            .ThenInclude(x => x.c)
             .Include("b.d")

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Entity Framework Core 6 中的 Simplify.Include 和.ThenInclude 调用 - Simplify .Include and .ThenInclude calls in Entity Framework Core 6 .ThenInclude 用于 Entity Framework Core 2 中的子实体 - .ThenInclude for sub entity in Entity Framework Core 2 然后在实体框架核心中包含显式加载? - ThenInclude for Explicit Loading in Entity Framework core? 网络核心:实体框架然后包含在Projection Select中 - Net Core: Entity Framework ThenInclude with Projection Select Entity Framework Core 如何在不使用 Include().ThenInclude() 的情况下从模型中从多对多列出一对多 - Entity Framework Core how to list one to many from many to many from a model without using Include().ThenInclude() Entity Framework Core - 我可以直接查询子属性中的列表/对象,避免多次重复包含/然后包含吗? - Entity Framework Core - Can I query lists/objects in child property directly avoiding multiple repeating Include/ThenInclude? 使用 ThenInclude() 的结果,而不是再次从 Include() 开始。 实体框架核心 5 - Using the result of ThenInclude() rather than starting at Include() again. Entity Framework Core 5 如何使用.net-core和Entity Framework Core和Identity从thenInclude中获取特定列? - How do i get specific columns from a thenInclude using .net-core and Entity Framework Core and Identity? Entity Framework Core:使用包含从相关表中获取数据 - Entity Framework Core : get data from related tables with include 包括实体框架核心(数据循环) - Include Entity Framework Core (Loop of data)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM