简体   繁体   English

EntityFramework Core 在结果中包含多级导航属性

[英]EntityFramework Core including multilevel navigation properties in results

In EF Core, we can use .Include and .ThenInclude methods to load related data in queries.在 EF Core 中,我们可以使用.Include.ThenInclude方法在查询中加载相关数据。 Let's take the example from the official documentation:我们以官方文档中的例子为例:

1. using (var context = new BloggingContext())
2. {
3.     var blogs = context.Blogs
4.         .Include(blog => blog.Posts)
5.            .ThenInclude(post => post.Author)
6.            .ThenInclude(author => author.Photo)
7.         .Include(blog => blog.Owner)
8.            .ThenInclude(owner => owner.Photo)
9.         .ToList();
10.}

In the example above, it included the Post.Author property and then the Author.Photo property using ThenInclude in lines 5 and 6.在上面的例子,它包括在Post.Author属性,然后Author.Photo使用属性ThenInclude在线路5和6。

But what if the Post entity has another navigation property that I want to include?但是如果Post实体有另一个我想要包含的导航属性怎么办? If I use ThenInclude after line 6, it will be relative to the Photo property and if I use Include it will be relative back to the Blogs property.如果我在第 6 行之后使用ThenInclude ,它将相对于Photo属性,如果我使用Include ,它将相对于Blogs属性。 Is there any way to solve this directly in the query statement?有什么办法可以直接在查询语句中解决这个问题吗?

You can repeat identical Include s as often as you like (and deem sensible):您可以随心所欲地重复相同的Include s(并且认为是明智的):

 var blogs = context.Blogs
     .Include(blog => blog.Posts)
        .ThenInclude(post => post.Author)
        .ThenInclude(author => author.Photo)
     .Include(blog => blog.Posts)
        .ThenInclude(post => post.Document)
     .Include(blog => blog.Posts)
        .ThenInclude(post => post. ...)

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

相关问题 EntityFramework Core:渴望加载派生类型的导航属性 - EntityFramework Core: Eager loading navigation properties of derived types 如何更新EntityFramework Core Code First对象,包括所有也是复杂对象的属性? - How to update EntityFramework Core Code First object including all properties which are also complex objects? Json响应不包含所有导航属性EntityFramework Core和ASP .NETCore Web API - Json response does not contain all the navigation properties EntityFramework Core and ASP .NETCore Web API EntityFramework Core 加入后得到多个结果 - EntityFramework Core get multiple results after join 如何使用包含导航属性的Entity Framework Core级联进行软删除? - How to make soft delete in cascade with Entity Framework Core including navigation properties? 包含导航属性时,如何阻止 Entity Framework Core 创建“自引用循环”? - How do I stop Entity Framework Core from creating “Self Referencing Loops” when including navigation properties? C# Entityframework core 2.1 查询不包括 where 条件 - C# Entityframework core 2.1 query not including where condition 如何创建具有复杂多级关系的 EF Core 导航属性? - How to create EF Core navigation property with complex multilevel relationship? EF Core中实体的导航属性 - Navigation Properties of Entities in EF Core EF Core 中的条件导航属性 - Conditional navigation properties in EF Core
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM