Entity-Framework-DBZ/EntityFrameworkCore.Data/DeadBallZoneLeagueDbContext.cs
Kevin Matsubara 1aae643a78 Add example query with tracking disabled.
This can also be done globally, see context file.
2025-04-05 21:29:48 +02:00

52 lines
2.8 KiB
C#

using Microsoft.EntityFrameworkCore;
using EntityFrameworkCore.Domain;
using Microsoft.Extensions.Logging;
namespace EntityFrameworkCore.Data;
public class DeadBallZoneLeagueDbContext : DbContext
{
private string DbPath;
public DeadBallZoneLeagueDbContext()
{
// In Ubuntu 24.04, the file is created here: /home/user/.local/share
var folder = Environment.SpecialFolder.LocalApplicationData;
var path = Environment.GetFolderPath(folder);
DbPath = Path.Combine(path, "DeadBallZoneLeague_EFCore.db");
}
public DbSet<Team> Teams { get; set; }
public DbSet<Coach> Coaches { get; set; }
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.
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Team>().HasData(
// Use hardcoded dates, not dynamic data such as new DateTimeOffset.UtcNow.DateTime for seeding.
new Team { TeamId = 1, Name = "Neo Delhi", CreatedDate = new DateTime(2025, 4, 4, 17, 7, 27, 33)},
new Team { TeamId = 2, Name = "Voodoo", CreatedDate = new DateTime(2025, 4, 4, 17, 7, 27, 33) },
new Team { TeamId = 3, Name = "Penal X", CreatedDate = new DateTime(2025, 4, 4, 17, 7, 27, 33) },
new Team { TeamId = 4, Name = "Neo Tokyo", CreatedDate = new DateTime(2025, 4, 4, 17, 7, 27, 33) },
new Team { TeamId = 5, Name = "Neo Barcelona", CreatedDate = new DateTime(2025, 4, 4, 17, 7, 27, 33) },
new Team { TeamId = 6, Name = "Neo Manchester", CreatedDate = new DateTime(2025, 4, 4, 17, 7, 27, 33) },
new Team { TeamId = 7, Name = "Neo Bangkok", CreatedDate = new DateTime(2025, 4, 4, 17, 7, 27, 33) },
new Team { TeamId = 8, Name = "Neo Amsterdam", CreatedDate = new DateTime(2025, 4, 4, 17, 7, 27, 33) },
new Team { TeamId = 9, Name = "Killaklowns", CreatedDate = new DateTime(2025, 4, 4, 17, 7, 27, 33) },
new Team { TeamId = 10, Name = "Sol", CreatedDate = new DateTime(2025, 4, 4, 17, 7, 27, 33) },
new Team { TeamId = 11, Name = "DEC", CreatedDate = new DateTime(2025, 4, 4, 17, 7, 27, 33) },
new Team { TeamId = 12, Name = "Leopards", CreatedDate = new DateTime(2025, 4, 4, 17, 7, 27, 33) },
new Team { TeamId = 13, Name = "Harlequins", CreatedDate = new DateTime(2025, 4, 4, 17, 7, 27, 33) },
new Team { TeamId = 14, Name = "Gladiators", CreatedDate = new DateTime(2025, 4, 4, 17, 7, 27, 33) },
new Team { TeamId = 15, Name = "Fiz-O", CreatedDate = new DateTime(2025, 4, 4, 17, 7, 27, 33) }
);
}
}