title: MVC5 Changing database connection string at runtime
body: <p>I am looking at using MVC5 to compliment an existing ASP.Net application (that is not MVC).</p>
  
  <p>The app has been configured using database first. </p>
  
  <p>I need to change the database connection string at runtime depending on who is logged in.  (This is easy to do in the current app through data adapters).</p>
  
  <p>There are over 1000 existing databases and more can created at runtime - so Web.config is not an option.  Schema is common to all databases.</p>
  
  <p>So far, I have managed to switch the database on the Controller - but this means changing substantially the generated code - there must be an easier way! - Help!</p>
  
  <p>Added comment:</p>
  
  <p>The model is a SaaS accounting application.  Each database stores all ledgers for a client company and has approximately 125 tables within it. They are kept separate for security and also portability (some accountants even require to download the SQL data in it's entirety for their clients).</p>
  
  <p>Example code:</p>
  
  <p>The site is built on the Contoso University model:
  <a href="http://www.asp.net/mvc/tutorials/mvc-5/database-first-development/setting-up-database" rel="nofollow">http://www.asp.net/mvc/tutorials/mvc-5/database-first-development/setting-up-database</a></p>
  
  <p>It works in its basic form, but I need the connection changed for each clients data:
  So looking at the Sales ledger - under the Sales controller we have:</p>
  
  <pre><code>public class SalesController : Controller
  {
  
      private Sales db = new Sales();
  </code></pre>
  
  <p>.....</p>
  
  <p>The Sales definition is:</p>
  
  <pre><code>public partial class Sales : DbContext
  {
  
      public Sales()
          : base("name=Sales")
      {                                                                        
      }
  </code></pre>
  
  <p>The "name=Sales" links to the WebConfig and gives it the starting database.</p>
  
  <p>Looking at the : base definition takes me to System.Data.Entity.DbContext which is a locked file!</p>
  
  <p>I have trawled through various sites and tried putting the connection string in the Sales class the following being one of the suggestions:</p>
  
  <p>public Sales(string connString) : base (connString)</p>
  
  <p>but this throws:</p>
  
  <p><em>Code generated using the T4 templates for Database First and Model First development may not work correctly if used in Code First mode. To continue using Database First or Model First ensure that the Entity Framework connection string is specified in the config file of executing application. To use these classes, that were from Database First or Model First, with Code First add any additional configuration using attributes or the DbModelBuilder API and then remove the code that throws this exception</em></p>
  
  <p>Line 32:         protected override void OnModelCreating(DbModelBuilder modelBuilder)</p>
  
  <p>Line 33:         {            </p>
  
  <p>Line 34:             throw new UnintentionalCodeFirstException();</p>
  
  <p>Line 35:         }</p>
  
  <p>Line 36:     </p>
  
  <p>That's where I am stuck - even if I use the same connection string as in the WebConfig!!</p>
  
  <p>(It mentions Code First mode - which it wasn't?)</p>