Add example query with tracking disabled.

This can also be done globally, see context file.
This commit is contained in:
Kevin Matsubara 2025-04-05 21:29:48 +02:00
parent 51eb8b117a
commit 1aae643a78
2 changed files with 15 additions and 0 deletions

View File

@ -14,6 +14,20 @@ var teamFour = await context.Teams.SingleOrDefaultAsync(t => t.TeamId == 3);
// Rather than raise: "System.InvalidOperationException: Sequence contains no elements."
var coach = await context.Coaches.FirstOrDefaultAsync();
async Task TrackedQuery()
{
// EF Core tracks objects that are returned by queries.
// This is less useful in disconnected applictations like APIs and Web applications.
var teams = await context.Teams
.AsNoTracking()
.ToListAsync();
foreach (var team in teams)
{
Console.WriteLine(team.Name);
}
}
async Task SkipAndTakeTeams()
{
var recordCount = 3;

View File

@ -21,6 +21,7 @@ public class DeadBallZoneLeagueDbContext : DbContext
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseSqlite($"Data source={DbPath}")
// .UseQueryTrackingBehavior(QueryTrackingBehavior.NoTracking) // Do not use tracking, set globally on all queries.
.LogTo(Console.WriteLine, LogLevel.Information)
.EnableSensitiveDataLogging() // Do not allow this in Production.
.EnableDetailedErrors(); // Do not allow this in Production.