Sanket Jain

Sanket Jain

  • 1.2k
  • 850
  • 97.6k

How to override appsettings.json with environment variables?

Jan 31 2020 1:26 AM
How to override appsettings.json with environment variables for connection string in onconfiguring method in dbcontext class in ASP.NET Core 3.0 project ?
 
I wrote below code in 
 
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
if (!optionsBuilder.IsConfigured)
{
IConfigurationRoot configuration = new ConfigurationBuilder()
.SetBasePath(AppDomain.CurrentDomain.BaseDirectory)
.AddJsonFile("appsettings.json")
.Build();
optionsBuilder.UseSqlServer(configuration.GetConnectionString("UserDBContext"));
 }
}
 

Brought to you by: JavaScript SDK for Bold BI dashboard and analytics embedding. Free trial.

Answers (1)