-
-
Save christiannagel/cb5e480a8fa031a5d862692904119ba6 to your computer and use it in GitHub Desktop.
Explicit Loading with EF Core
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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