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 options) : base(options) { // This constuctor allows it to accept the options set from other instances, such as in the API project. } public DbSet Teams { get; set; } public DbSet Coaches { get; set; } public DbSet Leagues { get; set; } public DbSet Matches { get; set; } public DbSet 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().HasNoKey().ToView("vw_TeamsAndLeagues"); // This is for user-defined functions. modelBuilder.HasDbFunction( typeof(DeadBallZoneLeagueDbContext) .GetMethod( nameof(GetEarliestTeamMatch), new[] {typeof(int)} )) .HasName("GetEarliestMatch"); } public DateTime GetEarliestTeamMatch(int teamId) => throw new NotImplementedException(); } public class DeadBallZoneLeagueDbContextFactory : IDesignTimeDbContextFactory { 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(); optionsBuilder.UseSqlite($"Data Source={dbPath}"); return new DeadBallZoneLeagueDbContext(optionsBuilder.Options); } }