I have created a development and a production environment as well as two appsettings[environment].json files. Both appsettings have different connection strings for a dev and live database. I am able to switch between environments easily enough but in doing so I was hoping that the environment would be pointing at the database relevant to my environment.
When creating the database using Entity Framework Core and any migrations, I am looking for a way to switch between connection strings depending on my envinronment. Below are my appsettings.json and launchSettings.json files. I am currently using ASP.NET Core 3.1...
{
"ConnectionStrings": {
"DevDb": "dev connection string here"
},
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Information"
}
},
"AllowedHosts": "*"
}
{
"ConnectionStrings": {
"Db": "Live Conn string here"
},
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Information"
}
},
"AllowedHosts": "*"
}
Launch Settings
"iisSettings": {
"windowsAuthentication": false,
"anonymousAuthentication": false,
"iisExpress": {
"applicationUrl": "http://localhost:50301",
"sslPort": 44314
}
},
"profiles": {
"DOLS (UAT)": {
"commandName": "Project",
"launchBrowser": true,
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development",
"ASPNETCORE_HOSTINGSTARTUPASSEMBLIES": "Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation"
}
},
"DOLS (LIV)": {
"commandName": "Project",
"launchBrowser": true,
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Production",
"ASPNETCORE_HOSTINGSTARTUPASSEMBLIES": "Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation"
},
"applicationUrl": ""
}
}
}
Startup class
public void ConfigureServices(IServiceCollection services)
{
services.AddRazorPages();
services.AddDbContext<ApplicationDbContext>(option => option.UseSqlServer(Configuration.GetConnectionString("Db")));
}
Program Files
public class Program
{
public static void Main(string[] args)
{
CreateHostBuilder(args).Build().Run();
}
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup<Startup>();
});
}
}
Any help is appreciated
Manage User Secretsinstead of twoappsettings.jsonlearn.microsoft.com/en-us/aspnet/core/security/…appsettings.Production.json. What you ask is how configuration andappsettings....jsonalready work. You don't need anything else. No secrets, no factories. In .NET Core Configuration multiple providers can provide the same setting and the last one wins.appsettins.production.jsonis loaded afterappsettings.json, s the `