Skip to content

Instantly share code, notes, and snippets.

@christiannagel
Last active January 27, 2019 19:23
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/cb5e480a8fa031a5d862692904119ba6 to your computer and use it in GitHub Desktop.
Save christiannagel/cb5e480a8fa031a5d862692904119ba6 to your computer and use it in GitHub Desktop.
Explicit Loading with EF Core
public void GetBooksWithExplicitLoading()
{
var books = _booksContext.Books.Where(b => b.Publisher.StartsWith("Wrox"));
foreach (var book in books)
{
Console.WriteLine(book.Title);
EntityEntry<Book> entry = _booksContext.Entry(book);
entry.Collection(b => b.Chapters).Load();
foreach (var chapter in book.Chapters)
{
Console.WriteLine($"{chapter.Number}. {chapter.Title}");
}
entry.Reference(b => b.Author).Load();
Console.WriteLine($"author: {book.Author?.Name}");
entry.Reference(b => b.Reviewer).Load();
Console.WriteLine($"reviewer: {book.Reviewer?.Name}");
entry.Reference(b => b.Editor).Load();
Console.WriteLine($"editor: {book.Editor?.Name}");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment