Sanket Jain

Sanket Jain

  • NA
  • 855
  • 140.2k

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"));
 }
}
 

Answers (1)