What is the best practice for implementing multiple databases in Asp.Net MVC 5?

asp.net asp.net-mvc-5 c# connection-string entity-framework-6

Question

I have a dropdown that displays all the databases in the Server. So When I click on some database It should use the particular database for the whole application. What is the best method to do that?

Should I create the connectionString dynamically or just add multiple ConnectionStrings in Web.Config?

1
2
11/9/2015 1:49:43 PM

Accepted Answer

This is more of an implementation technique.

In your case I am assuming the following:

1) The dropdown is filled dynamically from the available Db's in the database.

2) The connection to the server remains constant, only the db name needs to change.

If the above is true, you can store a template connection string in the config, e.g.:

<connectionStrings>
    <add name="sampleConn" connectionString="Data Source=<yourServer>;Initial Catalog={0};Integrated Security=true;" providerName="System.Data.SqlClient"/>
...

And then you can read in the code like:

var conString = string.Format(ConfigurationManager.ConnectionStrings["sampleConn"].ConnectionString, drpValue);

where drpValue is the db name from the dropdown.

0
11/9/2015 9:29:53 AM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow