title: Easily switching between connection strings in .NET Core
body: <p>I've got a code base that uses EF Core <em>and</em> Dapper to perform actions on a database.
  I want to set up a new copy of the site to develop some features and I want this site to connect to a new isolated copy of the database (<code>dbConnectionDEBUG</code>).</p>
  <p>At the moment, I use the following setup:</p>
  <p><strong>startup.cs</strong></p>
  <pre><code>...
  services.AddDbContext&lt;ApplicationDbContext&gt;(options =&gt;
      options.UseSqlServer(
          Configuration.GetConnectionString(&quot;dbConnectionMain&quot;)));
  
  services.Configure&lt;ConnectionStrings&gt;(Configuration.GetSection(&quot;ConnectionStrings&quot;));
  ...
  </code></pre>
  <p>I have a <code>ConnectionStrings</code> class which is being populated correctly via the DI in startup:</p>
  <pre><code>public class ConnectionStrings
  {
      public string dbConnectionMain { get; set; }
      public string dbConnectionDEBUG { get; set; }
  
      public ConnectionStrings()
      {
          this.dbConnectionMain = &quot;&quot;;
          this.dbConnectionDEBUG = &quot;&quot;;
      }
  }
  </code></pre>
  <p>Then, throughout my controllers/services I have access to <code>ConnectionStrings</code> and 99% of the time I'm doing the following to make DB calls:</p>
  <pre><code>using (var conn = new SqlConnection(_connectionStrings.dbConnectionMain))
  {
      conn.Open();
      ...
  </code></pre>
  <p>This would amount to a lot of code changes if I were to want to switch over to the 'DEBUG' db.</p>
  <p>How do I easily switch between the connection strings in my code depending on what version of the system I'm working on.
  If I could somehow do this dynamically that'd be great. The obvious determining factor would be the URL the site is operating on.</p>
  <p>Alternatively, (as a single change) do I just manually change the connection string at the source (e.g keystore/appsettings). I'm not keen on this as it leaves room for human error.</p>
  <hr />
  <h1>Update (2)</h1>
  <p>Based on what @Nkosi mentioned I am pursuing this path:</p>
  <ul>
  <li>Have one connection string 'Id' (i.e. <code>dbConnection</code>) used throughout</li>
  <li>Differentiate the connection string <em>value</em> within this based on the environment the app is running/deployed in</li>
  </ul>
  <p>I have another question:</p>
  <p>If I have the following...</p>
  <pre><code>&quot;MYAPPNAME&quot;: {
    &quot;commandName&quot;: &quot;Project&quot;,
    &quot;launchBrowser&quot;: true,
    &quot;environmentVariables&quot;: {
      &quot;ASPNETCORE_ENVIRONMENT&quot;: &quot;Development&quot;
    },
    &quot;applicationUrl&quot;: &quot;http://localhost:12345/;https://myapptestdomain.com/&quot;
  }
  </code></pre>
  <p>and:</p>
  <pre><code>WebHost.CreateDefaultBuilder(args)
      .ConfigureAppConfiguration((context, config) =&gt;
      {
          IHostingEnvironment env = context.HostingEnvironment;
  
          config.AddJsonFile($&quot;appsettings.{env.EnvironmentName.ToLower()}.json&quot;, optional: true);
          
      })
      .UseStartup&lt;Startup&gt;();
  </code></pre>
  <p>...will this automatically pick up my site is in the <code>Development</code> mode based on the <code>applicationUrl</code> values OR will I have to manually add <code>ASPNETCORE_ENVIRONMENT</code> with a value <code>Development</code> on the server I deploy the app to?</p>
  <p>Additional: My app is running in an Azure App Service.</p>
  <hr />
  <h1>Update (3) - Mission Complete</h1>
  <p>Just to finalise this question (in case anyone needs to know this), I have the following setup based on recommendations made by @Nkosi.</p>
  <ul>
  <li>Connection String - I have one connection string Id/name <code>dbConnection</code> which is used in all appSettings (see below)</li>
  <li>App Settings
  <ul>
  <li>I have a default <code>appSettings.json</code> with <code>dbConnection</code> that looks at the live database</li>
  <li>I have an additional <code>appSettings.Playground.json</code> file with <code>dbConnection</code> that looks at my testing database</li>
  </ul>
  </li>
  <li>Azure - App Service - On my <code>playground</code> development slot I have added an App Setting for ASPNETCORE_ENVIRONMENT with the value 'Playground'</li>
  <li>In my <code>Program.cs</code> file I have:</li>
  </ul>
  <p><code>config.AddJsonFile($&quot;appsettings.json&quot;, optional: true,reloadOnChange: true);</code></p>
  <p>and</p>
  <p><code>config.AddJsonFile($&quot;appsettings.{env.EnvironmentName.ToLower()}.json&quot;, optional: true,reloadOnChange: true);</code></p>
  <p>Just to note, I do also initialise a Vault on Azure which stores all my Keys and Secrets for the Azure based apps. Locally <code>User Secrets</code> is used.</p>