Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to switch between test and production databases with Entity Framework 5

I've got separate test and production databases for my ASP.NET MVC 4 application. Using Entity Framework 5, how can I have Entity Framework switch between these two databases programmatically? The application knows which database should be used at which time, so I just need for the application to be able to change Entity Framework so that it utilizes the correct database at the correct time.

Anyone know how to accomplish this and/or have a good example available?


EDIT: due to how my app is arranged, I am going to need to utlize GvM's answer. The last remaining thing, however, is that I don't know how to send an argument to the base class. Here is what I mean:

public class YourContext : DbContext {

    public YourContext() : base("yourNameOrConnectionString")
    {
    }

}

The problem is that this code does not work:

string dbConnectionString = "MyDBTest";

using (var db = new YourContext(dbConnectionString))
{
    //code to use the db
}

How does one send an argument to a base class, in this case YourContext() : base()

like image 398
flying227 Avatar asked Apr 21 '14 16:04

flying227


2 Answers

Use the config transforms. That's what they're there for. Admittedly, though, the config transforms are a little confusing because the debugger doesn't actually use them. Let me elaborate.

By default, you get three web.configs: Web.config, Web.Debug.config and Web.Release.config. The latter two, are combined with the first in the Solution Explorer, but you can expand Web.config to see them. Web.Debug.config and Web.Release.config are the transforms. They use a special XML-style syntax to allow you alter, or transform, settings in the main Web.config file. Each corresponds to a "Configuration", namely "Debug" and "Release", the two built into Visual Studio by default. You can add additional configurations as you need them.

Now, here's where things are confusing. The Debug configuration, despite its name, is never actually used when debugging. A better name would be perhaps Development or Staging; it's the configuration intended for when you deploy the site in a testing capacity, rather than to production. The Release config is for production. So, what you need is in your main Web.config, specify the connection string for your local development database. Then, in the Debug and Release configs, you add a transform to change that to the staging/production database connection strings, respectively. When you're debugging locally, the one the main web.config will be used, and then when you publish your application, you'll choose to either use Debug or Release, based on the environment you'll be deploying to, and then the transforms will be run to alter the published web.config appropriately.

For more information on transforms and how to write them, see: http://msdn.microsoft.com/en-us/library/dd465326(v=vs.110).aspx

like image 167
Chris Pratt Avatar answered Oct 03 '22 15:10

Chris Pratt


Provide your connection name or connection string to the DbContext constructor.

public class YourContext : DbContext {

    public YourContext(string connection) : base(connection)
    {
    }

}

The string can be a variable your application modifies based on what database you want to connect to.

EDIT: updated the example so that now you have a constructor that accepts a string parameter which then passes it to the base class when instantiated.

like image 41
GvM Avatar answered Oct 03 '22 14:10

GvM