title: The context is being used in Code First mode with code that was generated from an EDMX file
body: <p>I am developing an MVC 5 application with EF 6 database first approach. Now I want my DbContext to access multiple databases based on different Users. For that matter I generated a connection string in Web.Config file as:</p>
  
  <pre><code>string str = u.Users.Where(x =&gt; x.UserName == HttpContext.User.Identity.Name).First().ConnStr;
  var configuration = WebConfigurationManager.OpenWebConfiguration("~");
  var section = (ConnectionStringsSection)configuration.GetSection("connectionStrings");          
  ConnectionStringSettings c = new ConnectionStringSettings();    
  c.ConnectionString = str;
  c.Name = u.Users.Where(x =&gt; x.ConnStr == str).First().DbName;
  c.ProviderName = "System.Data.SqlClient";
  section.ConnectionStrings.Add(c);
  configuration.Save();
  </code></pre>
  
  <p>The connection string generated in config file is:</p>
  
  <pre><code>&lt;add name="Awais123" connectionString="Data Source=.;Initial Catalog=Awais123;integrated security=True;MultipleActiveResultSets=True"
        providerName="System.Data.SqlClient" /&gt;
  </code></pre>
  
  <p>And the Connection String generated by EF when I made EDMX is:</p>
  
  <pre><code>&lt;add name="ABCEntities" connectionString="metadata=res://*/Models.awais.csdl|res://*/Models.awais.ssdl|res://*/Models.awais.msl;provider=System.Data.SqlClient;provider connection string=&amp;quot;data source=.;initial catalog=Awais;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework&amp;quot;"
        providerName="System.Data.EntityClient" /&gt;
  </code></pre>
  
  <p>Then I overloaded my Entities constructor as:</p>
  
  <pre><code>public ABCEntities(string nameorConnString) : base(nameorConnString) {}
  </code></pre>
  
  <p>And the default Constructor is:</p>
  
  <pre><code>public ABCEntities() : base("name=ABCEntities") {}
  </code></pre>
  
  <p>Now I declare another DbContext class in another .cs file to generate the database as:</p>
  
  <pre><code>public partial class NewDbGen : DbContext
  {
       public NewDbGen(string nameorConnString): base(nameorConnString)
       { Database.Create(); }
  }
  </code></pre>
  
  <p>I am calling the DbContext objects as:</p>
  
  <pre><code>private static string str = u.Users.Where(x =&gt; x.UserName == HttpContext.User.Identity.Name).First().ConnStr;
  private NewDbGen de = new NewDbGen(str); // for creating a new database
  private ABCEntities db = new AbcEntities(str); // my original Entites.. and all my controllers use these entites for all queries
  </code></pre>
  
  <p>Now all this runs successfully and database is created in SQL Server. But later when I tried to query on that database an error occured in:</p>
  
  <pre><code>protected override void OnModelCreating(DbModelBuilder modelBuilder)
  {
       throw new UnintentionalCodeFirstException();
  }
  </code></pre>
  
  <p>And it throws an error </p>
  
  <blockquote>
    <p>The context is being used in Code First mode with code that was
    generated from an EDMX file for either Database First or Model First
    development.  This will not work correctly. To fix this problem do not
    remove the line of code that throws this exception. If you wish to use
    Database First or Model First, then make sure that the Entity
    Framework connection string is included in the app.config or
    web.config of the start-up project. If you are creating your own
    DbConnection, then make sure that it is an EntityConnection and not
    some other type of DbConnection, and that you pass it to one of the
    base DbContext constructors that take a DbConnection. To learn more
    about Code First, Database First, and Model First see the Entity
    Framework documentation here:
    <a href="http://go.microsoft.com/fwlink/?LinkId=394715" rel="nofollow noreferrer">http://go.microsoft.com/fwlink/?LinkId=394715</a></p>
  </blockquote>
  
  <p>What can I do? I have to use single DbContext for all User Databases and same object of ABCEntities i.e. "db" to access database in controller methods.
  I have already looked into <a href="https://stackoverflow.com/questions/32865627/using-multiple-databases-with-single-dbcontext-and-entites-and-generating-conn-s">this question</a> DbFactory method, but the same error occured.</p>
  
  <p>I want to make a Single Instance Multi-Tenant application</p>
  
  <p>Regards. </p>