From 7c989a2b67241da657caa8b5d4fe38defa3f73a2 Mon Sep 17 00:00:00 2001 From: maumar Date: Mon, 16 Nov 2020 11:13:08 -0800 Subject: [PATCH] Fix to #2873 - ThenInclude required? Added example for using single Include/ThenInclude method call to load multiple navigations. Fixes #2873 --- .../core/querying/related-data/eager.md | 5 +++++ samples/core/Querying/RelatedData/Program.cs | 22 ++++++++++++++----- 2 files changed, 21 insertions(+), 6 deletions(-) diff --git a/entity-framework/core/querying/related-data/eager.md b/entity-framework/core/querying/related-data/eager.md index 3e4ce44802..4e359ffaca 100644 --- a/entity-framework/core/querying/related-data/eager.md +++ b/entity-framework/core/querying/related-data/eager.md @@ -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] diff --git a/samples/core/Querying/RelatedData/Program.cs b/samples/core/Querying/RelatedData/Program.cs index fc75d07660..8bca1e3f44 100644 --- a/samples/core/Querying/RelatedData/Program.cs +++ b/samples/core/Querying/RelatedData/Program.cs @@ -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()) { @@ -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