Add example Explicit Loading function.

This commit is contained in:
Kevin Matsubara 2025-04-07 14:02:25 +02:00
parent 8103c682cf
commit 3652a69730

View File

@ -15,6 +15,29 @@ context.Database.EnsureCreated();
// Rather than raise: "System.InvalidOperationException: Sequence contains no elements."
// var firstCoach = await context.Coaches.FirstOrDefaultAsync();
await ExplicitLoadingExample();
async Task ExplicitLoadingExample()
{
var league = await context.FindAsync<League>(1);
if (!league.Teams.Any())
{
Console.WriteLine("Teams have not been loaded.");
}
await context.Entry(league)
.Collection(l => l.Teams)
.LoadAsync();
if (league.Teams.Any())
{
foreach (var team in league.Teams)
{
Console.WriteLine($"{team.Name}");
}
}
}
async Task EagerLoadingExample()
{
var leagues = await context.Leagues