remove seperate productiondbcontext to prevent having to change all of sodoffs code, instead determine which database to use in DBContext

This commit is contained in:
Alan Moon 2025-03-19 12:39:26 -07:00
parent d47acbbbb2
commit c41c5a34e8
2 changed files with 35 additions and 45 deletions

View File

@ -35,12 +35,17 @@ public class DBContext : DbContext {
public DbSet<Buddy> Buddies { get; set; } = null!;
private readonly IOptions<ApiServerConfig> config;
private readonly IWebHostEnvironment webHostEnvironment;
public DBContext(IOptions<ApiServerConfig> config) {
public DBContext(IOptions<ApiServerConfig> config, IWebHostEnvironment webHostEnvironment) {
this.config = config;
this.webHostEnvironment = webHostEnvironment;
}
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) {
if(webHostEnvironment.IsDevelopment())
{
#if USE_POSTGRESQL
if (config.Value.DbProvider == DbProviders.PostgreSQL) {
optionsBuilder.UseNpgsql(config.Value.DbConnection).UseLazyLoadingProxies();
@ -67,6 +72,25 @@ public class DBContext : DbContext {
#endif
throw new Exception($"Unsupported DbProvider {config.Value.DbProvider}");
}
else
{
#if USE_POSTGRESQL
if (config.Value.ProdDbProvider == DbProviders.PostgreSQL)
{
optionsBuilder.UseNpgsql(Environment.GetEnvironmentVariable("PRODUCTION_DB_CONNECTION")).UseLazyLoadingProxies();
return;
}
#endif
#if USE_MYSQL
if (config.Value.ProdDbProvider == DbProviders.MySQL)
{
optionsBuilder.UseMySQL(Environment.GetEnvironmentVariable("PRODUCTION_DB_CONNECTION")).UseLazyLoadingProxies();
return;
}
#endif
throw new Exception($"Unsupported Production DbProvider {config.Value.ProdDbProvider}");
}
}
protected override void OnModelCreating(ModelBuilder builder) {
// Sessions

View File

@ -1,34 +0,0 @@
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Options;
using sodoff.Configuration;
namespace sodoff.Model
{
public class ProductionDBContext : DBContext
{
private readonly IOptions<ApiServerConfig> config;
public ProductionDBContext(IOptions<ApiServerConfig> config) : base(config)
{
this.config = config;
}
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
#if USE_POSTGRESQL
if (config.Value.ProdDbProvider == DbProviders.PostgreSQL)
{
optionsBuilder.UseNpgsql(Environment.GetEnvironmentVariable("PRODUCTION_DB_CONNECTION")).UseLazyLoadingProxies();
return;
}
#endif
#if USE_MYSQL
if (config.Value.ProdDbProvider == DbProviders.MySQL)
{
optionsBuilder.UseMySQL(Environment.GetEnvironmentVariable("PRODUCTION_DB_CONNECTION")).UseLazyLoadingProxies();
return;
}
#endif
throw new Exception($"Unsupported Production DbProvider {config.Value.ProdDbProvider}");
}
}
}