title: How to connect with multiple databases with one entity framework dynamically.?
body: <p>This is my MVC project.I'm using entity framework 5. I have one main client database script.With that i can create many client databases with just the database name as different.Now I have an entity framework model for that client databases.With one entity framework i need to connect with different databases dynamically.i'll get the database name from one textbox.I know its about to get the connection string from web.config and changing the database name.But i could not find a solution like that.I tried with "SqlConnectionStringBuilder" too.I should use only one connection string in the web.config and that for the main client database.</p>
  
  <p>Here is my web.config connection string</p>
  
  <pre><code>    &lt;add name="SBAClientEntities" connectionString="metadata=res://*/Models.ClientModel.csdl|res://*/Models.ClientModel.ssdl|res://*/Models.ClientModel.msl;provider=System.Data.SqlClient;provider connection string=&amp;quot;data source=USER-PC\SQL2012SERVER1;initial catalog=SBAClient;persist security info=True;user id=sa;password=sa12345;MultipleActiveResultSets=True;App=EntityFramework&amp;quot;" providerName="System.Data.EntityClient" /&gt;
  </code></pre>
  
  <p>Here is my entity model Context class</p>
  
  <pre><code> public partial class SBAClientEntities : DbContext
  {
      public SBAClientEntities()
          : base("name=SBAClientEntities")
      {
      }
      public SBAClientEntities(string databaseName)
          : base("name=SBAClientEntities")
      {
      }
  
  
      protected override void OnModelCreating(DbModelBuilder modelBuilder)
      {
          throw new UnintentionalCodeFirstException();
      }
  
      public DbSet&lt;CM_Customer_UISetings&gt; CM_Customer_UISetings { get; set; }
  }
  </code></pre>
  
  <p>Here is the code to connect with database in repository</p>
  
  <pre><code> using (SBAClientEntities db1 = new SBAClientEntities(databaseName))
          {
              CusFontViewModel obj = new CusFontViewModel();
  
              var result = from c in db1.CM_Customer_UISetings
                           select c;
              obj.itemList = result.ToList();
              return obj;
          }
  </code></pre>