title: Switching connection strings depending on Environment in ASP.NET CORE
body: <p>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.</p>
  <p>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...</p>
  <pre><code>
  {
    &quot;ConnectionStrings&quot;: {
      &quot;DevDb&quot;: &quot;dev connection string here&quot;
    },
  
    &quot;Logging&quot;: {
      &quot;LogLevel&quot;: {
        &quot;Default&quot;: &quot;Information&quot;,
        &quot;Microsoft&quot;: &quot;Warning&quot;,
        &quot;Microsoft.Hosting.Lifetime&quot;: &quot;Information&quot;
      }
    },
    &quot;AllowedHosts&quot;: &quot;*&quot;
  }
  
  </code></pre>
  <pre><code>{
    &quot;ConnectionStrings&quot;: {
      &quot;Db&quot;: &quot;Live Conn string here&quot;
    },
  
    &quot;Logging&quot;: {
      &quot;LogLevel&quot;: {
        &quot;Default&quot;: &quot;Information&quot;,
        &quot;Microsoft&quot;: &quot;Warning&quot;,
        &quot;Microsoft.Hosting.Lifetime&quot;: &quot;Information&quot;
      }
    },
    &quot;AllowedHosts&quot;: &quot;*&quot;
  }
  
  </code></pre>
  <p>Launch Settings</p>
  <pre><code>
    &quot;iisSettings&quot;: {
      &quot;windowsAuthentication&quot;: false,
      &quot;anonymousAuthentication&quot;: false,
      &quot;iisExpress&quot;: {
        &quot;applicationUrl&quot;: &quot;http://localhost:50301&quot;,
        &quot;sslPort&quot;: 44314
      }
    },
    &quot;profiles&quot;: {
      &quot;DOLS (UAT)&quot;: {
        &quot;commandName&quot;: &quot;Project&quot;,
        &quot;launchBrowser&quot;: true,
        &quot;environmentVariables&quot;: {
          &quot;ASPNETCORE_ENVIRONMENT&quot;: &quot;Development&quot;,
          &quot;ASPNETCORE_HOSTINGSTARTUPASSEMBLIES&quot;: &quot;Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation&quot;
        }
      },
      &quot;DOLS (LIV)&quot;: {
        &quot;commandName&quot;: &quot;Project&quot;,
        &quot;launchBrowser&quot;: true,
        &quot;environmentVariables&quot;: {
          &quot;ASPNETCORE_ENVIRONMENT&quot;: &quot;Production&quot;,
          &quot;ASPNETCORE_HOSTINGSTARTUPASSEMBLIES&quot;: &quot;Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation&quot;
        },
        &quot;applicationUrl&quot;: &quot;&quot;
      }
    }
  }
  </code></pre>
  <p>Startup class</p>
  <pre><code> public void ConfigureServices(IServiceCollection services)
              {
              services.AddRazorPages();
  
              services.AddDbContext&lt;ApplicationDbContext&gt;(option =&gt; option.UseSqlServer(Configuration.GetConnectionString(&quot;Db&quot;)));
             
  
              }
  </code></pre>
  <p>Program Files</p>
  <pre><code>  public class Program
          {
          public static void Main(string[] args)
              {
              CreateHostBuilder(args).Build().Run();
              }
  
          public static IHostBuilder CreateHostBuilder(string[] args) =&gt;
              Host.CreateDefaultBuilder(args)
                  .ConfigureWebHostDefaults(webBuilder =&gt;
                  {
                      webBuilder.UseStartup&lt;Startup&gt;();
                  });
          }
      }
  
  
  </code></pre>
  <p>Any help is appreciated</p>