Eager Load many to Many - EF Core

Eager Load many to Many - EF Core

In Entity Framework Core, you can use the Include method to eagerly load related entities. To eager load a many-to-many relationship, you need to include both the intermediate entity and the target entity.

Assuming you have a many-to-many relationship between Book and Author, with an intermediate entity BookAuthor, you can eagerly load the authors for a book like this:

using Microsoft.EntityFrameworkCore; using System.Linq; // ... var book = context.Books .Include(b => b.BookAuthors) .ThenInclude(ba => ba.Author) .Single(b => b.BookId == bookId); var authors = book.BookAuthors.Select(ba => ba.Author).ToList(); 

In this example, we first include the BookAuthors navigation property of the Book entity, and then use ThenInclude to include the Author navigation property of the intermediate BookAuthor entity. Finally, we select the Author entities from the loaded BookAuthor entities.

Note that when using Include and ThenInclude, you should be careful to avoid the "N+1" problem, where the database is queried once for each entity in a collection. To avoid this, you can use the Select method to load only the data you need, or use the Load method to explicitly load related data.

Examples

  1. "EF Core Eager Loading Many-to-Many Relationship"

    Code:

    // EF Core Eager Loading Many-to-Many Relationship var authorsWithBooks = context.Authors.Include(a => a.AuthorBooks).ThenInclude(ab => ab.Book).ToList(); 

    Description: Eagerly load many-to-many relationships between Authors and Books using Include and ThenInclude methods.

  2. "EF Core Eager Loading Many-to-Many with Filter"

    Code:

    // EF Core Eager Loading Many-to-Many with Filter var authorsWithFilteredBooks = context.Authors .Include(a => a.AuthorBooks.Where(ab => ab.Book.PublishedYear > 2020)) .ToList(); 

    Description: Eagerly load many-to-many relationships with a filter condition using Include and Where in EF Core.

  3. "EF Core Eager Loading Many-to-Many with Additional Navigation Property"

    Code:

    // EF Core Eager Loading Many-to-Many with Additional Navigation Property var authorsWithBooksAndGenres = context.Authors .Include(a => a.AuthorBooks).ThenInclude(ab => ab.Book).ThenInclude(b => b.Genre) .ToList(); 

    Description: Eagerly load many-to-many relationships with additional navigation property (Genre) using Include and ThenInclude.

  4. "EF Core Eager Loading Many-to-Many with Multiple Includes"

    Code:

    // EF Core Eager Loading Many-to-Many with Multiple Includes var authorsWithBooksAndPublishers = context.Authors .Include(a => a.AuthorBooks).ThenInclude(ab => ab.Book).ThenInclude(b => b.Publisher) .ToList(); 

    Description: Eagerly load many-to-many relationships with multiple included entities (Books and Publisher) using Include and ThenInclude.

  5. "EF Core Eager Loading Many-to-Many with Projection"

    Code:

    // EF Core Eager Loading Many-to-Many with Projection var authorsWithBookTitles = context.Authors .Include(a => a.AuthorBooks).ThenInclude(ab => ab.Book) .Select(a => new { a.Name, BookTitles = a.AuthorBooks.Select(ab => ab.Book.Title) }) .ToList(); 

    Description: Eagerly load many-to-many relationships with a projection to select specific data using Include and Select.

  6. "EF Core Eager Loading Many-to-Many with Filtered Include"

    Code:

    // EF Core Eager Loading Many-to-Many with Filtered Include var authorsWithFilteredBooks = context.Authors .Include(a => a.AuthorBooks.Where(ab => ab.Book.PublishedYear > 2020).Select(ab => ab.Book)) .ToList(); 

    Description: Eagerly load many-to-many relationships with a filtered include using Include and Where with a select statement.

  7. "EF Core Eager Loading Many-to-Many with PredicateBuilder"

    Code:

    // EF Core Eager Loading Many-to-Many with PredicateBuilder var predicate = PredicateBuilder.New<AuthorBook>(ab => ab.Book.PublishedYear > 2020); var authorsWithFilteredBooks = context.Authors .Include(a => a.AuthorBooks.Where(predicate)) .ToList(); 

    Description: Eagerly load many-to-many relationships with a filter condition using PredicateBuilder and Include.

  8. "EF Core Eager Loading Many-to-Many with Select Loading"

    Code:

    // EF Core Eager Loading Many-to-Many with Select Loading var authorsWithBooks = context.Authors .Select(a => new { a, Books = a.AuthorBooks.Select(ab => ab.Book) }) .ToList(); 

    Description: Eagerly load many-to-many relationships using Select Loading to specify related entities to load.

  9. "EF Core Eager Loading Many-to-Many with IncludeList"

    Code:

    // EF Core Eager Loading Many-to-Many with IncludeList var authorsWithBooks = context.Authors .IncludeList(a => a.AuthorBooks.Select(ab => ab.Book)) .ToList(); 

    Description: Eagerly load many-to-many relationships using a custom extension method IncludeList to simplify loading.

  10. "EF Core Eager Loading Many-to-Many with Explicit Loading"

    Code:

    // EF Core Eager Loading Many-to-Many with Explicit Loading var authors = context.Authors.ToList(); foreach (var author in authors) { context.Entry(author).Collection(a => a.AuthorBooks).Load(); foreach (var authorBook in author.AuthorBooks) { context.Entry(authorBook).Reference(ab => ab.Book).Load(); } } 

    Description: Eagerly load many-to-many relationships using explicit loading for more control over the loading process.


More Tags

math application-loader capslock multer spell-checking cart jsch django-permissions asp.net-core-2.2 laravel-passport

More C# Questions

More Auto Calculators

More Stoichiometry Calculators

More Financial Calculators

More Electrochemistry Calculators