I have 10 databases with identical schema (appx. 20 tables). I am using MVC5 and Entity Framework 6. I am required to change between these databases based on user input. I.e. By selecting a year database associated with this year needs to be selected.
I have succeed at this task by manipulating web.config file using following code:
public static bool setDBConnectionString(string path)
{
try
{
string dataSource = @"metadata=res://*/Models.Micropay.Micropay.csdl|res://*/Models.Micropay.Micropay.ssdl|res://*/Models.Micropay.Micropay.msl;provider=VfpEntityFrameworkProvider2;provider connection string='data source=" + path + "'";
var configuration = WebConfigurationManager.OpenWebConfiguration("~");
var section = (ConnectionStringsSection)configuration.GetSection("connectionStrings");
section.ConnectionStrings["connectionStringEntity"].ConnectionString = dataSource;
configuration.Save(ConfigurationSaveMode.Modified);
ConfigurationManager.RefreshSection("connectionStrings");
}
catch
{
return false;
}
return true;
}
However, this is not optimal solution based on scale of the project and number of users.
I've tried rebuilding connection string using following code:
Debug.WriteLine(this.Database.Connection.ConnectionString);
var entityCnxStringBuilder = new EntityConnectionStringBuilder
(System.Configuration.ConfigurationManager
.ConnectionStrings["connectionStringEntity"].ConnectionString);
var sqlCnxStringBuilder = new SqlConnectionStringBuilder
(entityCnxStringBuilder.ProviderConnectionString);
sqlCnxStringBuilder.DataSource = @"C:\location";
this.Database.Connection.ConnectionString
= sqlCnxStringBuilder.ConnectionString;
this.Database.Connection.Open();
but I was unable to get around following error message:
"Multiple-step OLE DB operation generated errors. Check each OLE DB status value, if available. No work was done."
Any help would be much appreciated.