Skip to content

Instantly share code, notes, and snippets.

@christiannagel
Created January 27, 2019 18:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save christiannagel/775e68a33388e0c9f2d48144f48e4964 to your computer and use it in GitHub Desktop.
Save christiannagel/775e68a33388e0c9f2d48144f48e4964 to your computer and use it in GitHub Desktop.
Eager loading with EF Core
public void GetBooksWithEagerLoading()
{
var books = _booksContext.Books
.Where(b => b.Publisher.StartsWith("Wrox"))
.Include(b => b.Chapters)
.Include(b => b.Author)
.Include(b => b.Reviewer)
.Include(b => b.Editor);
foreach (var book in books)
{
Console.WriteLine(book.Title);
foreach (var chapter in book.Chapters)
{
Console.WriteLine($"{chapter.Number}. {chapter.Title}");
}
Console.WriteLine($"author: {book.Author?.Name}");
Console.WriteLine($"reviewer: {book.Reviewer?.Name}");
Console.WriteLine($"editor: {book.Editor?.Name}");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment