0

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);
}
Sean
  • 125
  • 8
  • Possible duplicate of [Entity Framework Core 2.1 - Multiple Providers](https://stackoverflow.com/questions/52365060/entity-framework-core-2-1-multiple-providers) – Jan Paolo Go Jun 13 '19 at 14:37
  • Similar, but it does not provide support for multiple environments. (dev,stage,prod) – Sean Jun 13 '19 at 14:53
  • Yes but I think you're on the right path by defining those keys in your appsettings.*.json files. There's some [special prefixes](https://learn.microsoft.com/en-us/aspnet/core/fundamentals/configuration/index?view=aspnetcore-2.2#prefixes) you could use but it may be only applicable in Azure. Then here's how you [migrate with multiple providers in EF Core](https://learn.microsoft.com/en-us/ef/core/managing-schemas/migrations/providers).. Hope those helps – Jan Paolo Go Jun 13 '19 at 16:42

0 Answers0