Add example Eager Loading function.
This commit is contained in:
parent
64965c3fef
commit
8103c682cf
@ -15,6 +15,23 @@ context.Database.EnsureCreated();
|
||||
// Rather than raise: "System.InvalidOperationException: Sequence contains no elements."
|
||||
// var firstCoach = await context.Coaches.FirstOrDefaultAsync();
|
||||
|
||||
async Task EagerLoadingExample()
|
||||
{
|
||||
var leagues = await context.Leagues
|
||||
.Include(l => l.Teams)
|
||||
.ThenInclude(t => t.Coach)
|
||||
.ToListAsync();
|
||||
|
||||
foreach (var league in leagues)
|
||||
{
|
||||
Console.WriteLine($"League - {league.Name}");
|
||||
foreach (var team in league.Teams)
|
||||
{
|
||||
Console.WriteLine($"* {team.Name} - {team.Coach.Name}");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async Task InsertRelatedData()
|
||||
{
|
||||
// Insert record with a Foreign Key.
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user