2

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=&quot;data source=USER-PC\SQL2012SERVER1;initial catalog=SBAClient;persist security info=True;user id=sa;password=sa12345;MultipleActiveResultSets=True;App=EntityFramework&quot;" 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;
        }

1 Answer 1

1
SBAClientEntities dbWILLBECHANGED =  new  SBAClientEntities();
dbWILLBECHANGED.Database.Connection.Open();
dbWILLBECHANGED.Database.Connection.ChangeDatabase(DatabaseNEWName);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.