Skip to content

Commit

Permalink
Fix to #2873 - ThenInclude required?
Browse files Browse the repository at this point in the history
Added example for using single Include/ThenInclude method call to load multiple navigations.
  • Loading branch information
maumar committed Nov 16, 2020
1 parent 1388fe7 commit 0d42eb1
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 6 deletions.
5 changes: 5 additions & 0 deletions entity-framework/core/querying/related-data/eager.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@ You may want to include multiple related entities for one of the entities that i

[!code-csharp[Main](../../../../samples/core/Querying/RelatedData/Program.cs#MultipleLeafIncludes)]

> [!TIP]
> You can also load multiple navigations using a single `Include` method. This is possible for navigation "chains" that are all references, or when they end with a single collection.
[!code-csharp[Main](../../../../samples/core/Querying/RelatedData/Program.cs#IncludeMultipleNavigationsWithSingleInclude)]

## Filtered include

> [!NOTE]
Expand Down
22 changes: 16 additions & 6 deletions samples/core/Querying/RelatedData/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,19 @@ static void Main(string[] args)
}
#endregion

#region IncludeTree
using (var context = new BloggingContext())
{
var blogs = context.Blogs
.Include(blog => blog.Posts)
.ThenInclude(post => post.Author)
.ThenInclude(author => author.Photo)
.Include(blog => blog.Owner)
.ThenInclude(owner => owner.Photo)
.ToList();
}
#endregion

#region MultipleLeafIncludes
using (var context = new BloggingContext())
{
Expand All @@ -77,15 +90,12 @@ static void Main(string[] args)
}
#endregion

#region IncludeTree
#region IncludeMultipleNavigationsWithSingleInclude
using (var context = new BloggingContext())
{
var blogs = context.Blogs
.Include(blog => blog.Posts)
.ThenInclude(post => post.Author)
.ThenInclude(author => author.Photo)
.Include(blog => blog.Owner)
.ThenInclude(owner => owner.Photo)
.Include(blog => blog.Owner.AuthoredPosts)
.ThenInclude(post => post.Blog.Owner.Photo)
.ToList();
}
#endregion
Expand Down

0 comments on commit 0d42eb1

Please sign in to comment.