I want to target a specific DbContext provider based on environment configuration. (i.e. appsettings.sqlserver.dev.json, appsettings.oracle.dev.json,appsettings.sqlserver.prod.json, appsettings.oracle.prod.json etc...)
The solution below works, but it seems like a hack. Furthermore, EF seems to ignore the environment configuration file convention, so you will need to maintain a default appsettings.json for EF commands. (add-migration,remove-migratin,update-database, etc...)
Are there any provider environment settings that can be used to accomplish this?
Startup.cs
public void ConfigureServices(IServiceCollection services)
{
string dbProvider = Configuration["Database:Context:Provider"];
string dbConnectionString = Configuration["Database:Context:ConnectionString"];
services.AddDbContext<DatabaseContext>(options =>
{
if (dbProvider == "SqlServer")
{
options.UseSqlServer(dbConnectionString, b => b.MigrationsAssembly("Eagle.Core.Web.Api"));
}
else if (dbProvider == "Oracle")
{
options.UseOracle(dbConnectionString, b => b.MigrationsAssembly("Eagle.Core.Web.Api"));
}
options.EnableSensitiveDataLogging(true);
});
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
}