I am developing an MVC 5 application. Initially I was using single database with EF6 DataBase first approach and I am using my DbContext instance to access my database which have 102 tables. I am declaring its instance as:
private MyEntities db = new MyEntities();
Now I want to allow multiple companies to use my application and for that matter I have to create a new database for each new company. I achieved this by declaring another constructor to my DbContext as follows:
public partial class NewEntities : DbContext
{
public NewEntities(string name)
: base(name)
{
}
}
And then declaring its instance as:
public NewEntities de = new NewEntities((ConfigurationManager.ConnectionStrings["NewEntities123"]).ToString());
Then I called
db.Database.Create();
and my new database was created successfully. But in this scheme I have to declare a new connection string in my Web.config file each time I want to add a database.
Is there any method where a connection string is generated automatically as per the company name in config file and then passed to the constructor to create a new database with that name?
Moreover I want to access all my controller methods with the same "db" instance and DbContext for every company, so that my same code can be used for all companies. How can I access single DbContext and its instance for multiple databases?
I have already looked answer to this as it is saying that there is no method. But then How can my application work for multiple Users?