46 lines
1.8 KiB
C#
46 lines
1.8 KiB
C#
using Microsoft.EntityFrameworkCore;
|
|
|
|
using EntityFrameworkCore.Domain;
|
|
using Microsoft.Extensions.Logging;
|
|
using System.Reflection;
|
|
|
|
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; }
|
|
public DbSet<League> Leagues { get; set; }
|
|
public DbSet<Match> Matches { get; set; }
|
|
public DbSet<TeamsAndLeaguesView> TeamsAndLeaguesView { 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.ApplyConfiguration(new TeamConfiguration());
|
|
// modelBuilder.ApplyConfiguration(new LeagueConfiguration());
|
|
|
|
// This line can be used, then individual configurations do not need to be loaded.
|
|
modelBuilder.ApplyConfigurationsFromAssembly(Assembly.GetExecutingAssembly());
|
|
|
|
// This database object does not have a Primary Key, so we state that here, otherwise an exception is thrown.
|
|
modelBuilder.Entity<TeamsAndLeaguesView>().HasNoKey().ToView("vw_TeamsAndLeagues");
|
|
}
|
|
}
|