Entity-Framework-DBZ/EntityFrameworkCore.Data/DeadBallZoneLeagueDbContext.cs

104 lines
3.9 KiB
C#

using Microsoft.EntityFrameworkCore;
using EntityFrameworkCore.Domain;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;
using System.Reflection;
using Microsoft.EntityFrameworkCore.Design;
namespace EntityFrameworkCore.Data;
public class DeadBallZoneLeagueDbContext : DbContext
{
public DeadBallZoneLeagueDbContext(DbContextOptions<DeadBallZoneLeagueDbContext> options) : base(options)
{
// This constuctor allows it to accept the options set from other instances, such as in the API project.
}
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 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");
// This is for user-defined functions.
modelBuilder.HasDbFunction(
typeof(DeadBallZoneLeagueDbContext)
.GetMethod(
nameof(GetEarliestTeamMatch),
new[] {typeof(int)}
))
.HasName("GetEarliestMatch");
}
// This is an example override for SQL Server:
// protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) => optionsBuilder.UseSqlServer("Data Source=(localdb)\\MSSQLLocalDB;Initial Catalog=FootballLeague_EfCore; Encrypt=true", sqlOptions => {
// sqlOptions.EnableRetryOnFailure(maxRetryCount: 5,
// maxRetryDelay: TimeSpan.FromSeconds(5),
// errorNumbersToAdd: null);
// });
protected override void ConfigureConventions(ModelConfigurationBuilder configurationBuilder)
{
// These apply to all the settings for all models.
configurationBuilder.Properties<string>().HaveMaxLength(100);
configurationBuilder.Properties<decimal>().HavePrecision(16, 2);
}
public override Task<int> SaveChangesAsync(CancellationToken cancellationToken = default)
{
var entries = ChangeTracker.Entries<BaseDomainModel>().Where(
λ => λ.State == EntityState.Modified || λ.State == EntityState.Added
);
// Whenever a save is made to an entity, these fields are now automatically updated:
foreach (var entry in entries)
{
entry.Entity.ModifiedDate = DateTime.UtcNow;
entry.Entity.ModifiedBy = "Sample User 1";
if (entry.State == EntityState.Added)
{
entry.Entity.CreatedDate = DateTime.UtcNow;
entry.Entity.CreatedBy = "Sample User";
}
// Update the concurrency token.
entry.Entity.Version = Guid.NewGuid();
}
return base.SaveChangesAsync(cancellationToken);
}
public DateTime GetEarliestTeamMatch(int teamId) => throw new NotImplementedException();
}
public class DeadBallZoneLeagueDbContextFactory : IDesignTimeDbContextFactory<DeadBallZoneLeagueDbContext>
{
public DeadBallZoneLeagueDbContext CreateDbContext(string[] args)
{
var folder = Environment.SpecialFolder.LocalApplicationData;
var path = Environment.GetFolderPath(folder);
IConfigurationRoot configuration = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json")
.Build();
var dbPath = Path.Combine(path, configuration.GetConnectionString("SqliteDatabaseName"));
var optionsBuilder = new DbContextOptionsBuilder<DeadBallZoneLeagueDbContext>();
optionsBuilder.UseSqlite($"Data Source={dbPath}");
return new DeadBallZoneLeagueDbContext(optionsBuilder.Options);
}
}