forked from SoDOff-Project/sodoff
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:
parent
d47acbbbb2
commit
c41c5a34e8
@ -35,37 +35,61 @@ 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 USE_POSTGRESQL
|
||||
|
||||
if(webHostEnvironment.IsDevelopment())
|
||||
{
|
||||
#if USE_POSTGRESQL
|
||||
if (config.Value.DbProvider == DbProviders.PostgreSQL) {
|
||||
optionsBuilder.UseNpgsql(config.Value.DbConnection).UseLazyLoadingProxies();
|
||||
return;
|
||||
}
|
||||
#endif
|
||||
#if USE_MYSQL
|
||||
#endif
|
||||
#if USE_MYSQL
|
||||
if (config.Value.DbProvider == DbProviders.MySQL) {
|
||||
optionsBuilder.UseMySQL(config.Value.DbConnection).UseLazyLoadingProxies();
|
||||
return;
|
||||
}
|
||||
#endif
|
||||
#if USE_SQLITE
|
||||
#endif
|
||||
#if USE_SQLITE
|
||||
if (config.Value.DbProvider == DbProviders.SQLite) {
|
||||
string DbPath;
|
||||
if (String.IsNullOrEmpty(config.Value.DbPath)) {
|
||||
if (String.IsNullOrEmpty(config.Value.DbPath)) {
|
||||
DbPath = Path.Join(Directory.GetCurrentDirectory(), "sodoff.db");
|
||||
} else {
|
||||
} else {
|
||||
DbPath = config.Value.DbPath;
|
||||
}
|
||||
}
|
||||
optionsBuilder.UseSqlite($"Data Source={DbPath}").UseLazyLoadingProxies();
|
||||
return;
|
||||
}
|
||||
#endif
|
||||
throw new Exception($"Unsupported DbProvider {config.Value.DbProvider}");
|
||||
#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) {
|
||||
|
@ -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}");
|
||||
}
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user