title: How to change db connection programmatically c# Entity Framework
body: <p>I've built my project and named the db connection "VMaxEntities" </p>
  
  <p>The connection string exists in web.config.</p>
  
  <p>I have another connection string "Development_VMaxEntities"</p>
  
  <p>Whenever I call the db, I use the code <code>using (VMaxEntities db = new VMaxEntities())</code></p>
  
  <p>This calls: </p>
  
  <p><code>public partial class VMaxEntities : DbContext
      {
          public VMaxEntities()
              : base("name=VMaxEntities")
          {
          }</code></p>
  
  <p>What I want to do is connect to a development database instead of the live one, IF the current URI contains localhost.</p>
  
  <p>So - thanks to cgotberg's answer below, here's what I used to get it working: (note, I add the password here instead of in web.config)</p>
  
  <pre><code> public VMaxEntities()
          : base("name=Secure_VMaxEntities")
      {
          if (System.Web.HttpContext.Current.Request.Url.Host == "localhost")
          {
              var connectionString = this.Database.Connection.ConnectionString + ";password=***********";
              this.Database.Connection.ConnectionString = connectionString.Replace("catalog=VMax", "catalog=DEV_VMax");
          }
          else
          {
              this.Database.Connection.ConnectionString += ";password=************";
          }
      }
  </code></pre>