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.
Here is my web.config connection string
<add name="SBAClientEntities" connectionString="metadata=res://*/Models.ClientModel.csdl|res://*/Models.ClientModel.ssdl|res://*/Models.ClientModel.msl;provider=System.Data.SqlClient;provider connection string="data source=USER-PC\SQL2012SERVER1;initial catalog=SBAClient;persist security info=True;user id=sa;password=sa12345;MultipleActiveResultSets=True;App=EntityFramework"" providerName="System.Data.EntityClient" />
Here is my entity model Context class
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<CM_Customer_UISetings> CM_Customer_UISetings { get; set; }
}
Here is the code to connect with database in repository
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;
}