1

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.

1
  • 2
    Do not change the web.config. Simply store the selected connection string in the user's Session or a cookie or some other persistent storage, then pass the connection string to your context. Commented May 22, 2015 at 16:05

1 Answer 1

3

why dont you use the other overload of the DbContext class. This overload's signature is DbContext(string). So you can manually pass the connection string into the class object. Also if you are not ok with the passing connection string around in the code. add all the string to the Web.Config and pass their name along.

So after that your code will be like.

string userInput // actul user input
var connS = SomeLogicClass.GetConnString(userInput);
using(var db = new YourDbContext:DbContext){
  //do your operations
}
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.