1

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.

2 Answers 2

3

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"]);
Sign up to request clarification or add additional context in comments.

4 Comments

But How will this constructor creates a new database. I have to create a new one before.
EF will create. and seed the database for you, with the correct settings.
So, how to pass nameOrConnectionString parameter??
I have added as it is said but nothing happens.. When I inserted a break point at the location, even Visual Studio didn't break in there.
2

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();

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.