title: Change Connection String at Runtime in EF 6
body: <p>I mostly use my dbcontext with using statements like this:</p>
  
  <pre><code>using (var context = new MyContext())
  {
  }
  </code></pre>
  
  <p>But I later had to add a project with a function to change the database. I found out that I could change my dbcontext constructor to this:</p>
  
  <pre><code>public MyContext(string connectionstring)
          : base(connectionstring)
  {
  }
  </code></pre>
  
  <p>I made a class with a property for the connectionstring:</p>
  
  <pre><code>public static class DbAccessor
  {
      public static string ConnectionStringForContext { get; set; }
  }
  </code></pre>
  
  <p>I set this property in the beginning and my using statements look like this:</p>
  
  <pre><code>using (var context = new MyContext(DbAccessor.ConnectionStringForContext) 
  {}
  </code></pre>
  
  <p>It works, but my connection string is now everywhere and I feel like I should not do that with connection strings. Is there a better way of doing this? What would be the best way to handle my Connection Strings if I want to change them? Should I stop using these using statements?</p>
  
  <p>I've seen this question: <a href="https://stackoverflow.com/questions/20216147/entity-framework-change-connection-at-runtime">Entity Framework change connection at runtime</a></p>
  
  <p>But the difference is, that I have a functioning solution, but I don't want to have my connection string everywhere. If I used that extension method inside every using statement. It would almost be like my solution just one line more in every using statement...</p>