Awais Mahmood
Awais Mahmood

Reputation: 1336

Creating multiple databases using EF in MVC and passing the Database name to InitialCatalogue as a variable in web.config

I am developing an MVC 5 application which is using 2 code first databases and 1 database first. Now I want to deploy and make it available for multiple clients to work. For that matter I want to generate my Database First Database for every new client. My database first database has 103 tables with PKs, FKs and triggers have also been built in them.

My DbContext is as follows:

public partial class MyEntities : DbContext
{
    public MyEntities() : base("name=MyEntities")
    {
    }
    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        throw new UnintentionalCodeFirstException();
    }
    ** Here are my DbSet's **
}

Now the major concern of mine is that I also want to use MyEntities for accessing the databases for each client. Because my all controllers validations are accessing the database with that entity name as follows:

public MyEntities db = new MyEntities();

Now how could I be able to create a new database for each client and the Context name should remain same so that I don't have to change my code in Controllers? Moreover I also have to pass the InitialCatalogue name as a variable to web.config for each database.

Upvotes: 1

Views: 366

Answers (2)

user6661908
user6661908

Reputation:

How to pass initial catalog as a variable name?

You can do this as;

<add name="MyDBContext" connectionString="metadata=res://*/model.csdl|res://*/model.ssdl|res://*/model.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=.;Initial Catalog={0};uid=sa; pwd=password;MultipleActiveResultSets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient"/>

First of all you will overload your entities constructor as answered by sth. Now on user login you have to put their database name in a session variable. Lets call it "dbName". So now you can access it as:

var connection = System.Configuration.ConfigurationManager.ConnectionStrings["MyDBContext"].ConnectionString;
var connectionString = string.Format(connection, System.Web.HttpContext.Current.Session["dbName"].ToString());
var db = new YourEntities(connectionString);
db.Database.CreateIfNotExists();

Upvotes: 2

sth
sth

Reputation: 146

You have to add one more constructor to your DbContext as follows :

public partial class MyEntities : DbContext
{
    public MyEntities(string nameOrConnectionString)
        : base(nameOrConnectionString)
    {
    }
}

You should add this constructor in a separate *.cs file as the previous one is auto-generated. Also make sure the namespace should be same as the auto-generated code.

Now you can instantiate the DbContext class using the desired database's connection string from the web.config as follows :

public MyEntities db = new MyEntities(ConfigurationManager.ConnectionStrings["database1"]);

Upvotes: 3

Related Questions