Now, this works fine, but as you can see I’m calling .Include(u = u.Posts) twice. Is there a way to call ThenInclude twice on same property, without actually writing the Include statement also twice?
Calling Include(u => u.Posts) twice is the right way to do it.
From EF Core docs… emphasis on the last sentence.
You may want to include multiple related entities for one of the entities that is being included. For example, when querying
Blogs, you includePostsand then want to include both theAuthorandTagsof thePosts. To do this, you need to specify each include path starting at the root. For example,Blog -> Posts -> AuthorandBlog -> Posts -> Tags. This does not mean you will get redundant joins, in most cases EF will consolidate the joins when generating SQL.
using (var context = new BloggingContext())
{
var blogs = context.Blogs
.Include(blog => blog.Posts)
.ThenInclude(post => post.Author)
.Include(blog => blog.Posts)
.ThenInclude(post => post.Tags)
.ToList();
}