views:

111

answers:

3

I have used Ruby on Rails with ActiveRecord, so I am quite used to switching to Production/Development database.

I am wondering, how do people implement Development and Production Database difference in ASP.NET MVC (preferably with ado.net entity).

I tried to have it by creating 2 entity datasets with the same name in a different namespace, so I would only need to switch the used namespace in my database code. Unfortunately, this didn't work because the entity datasets do not allow equal names...

+1  A: 

The easiest way (though it's still not as easy as in Rails) will be to include connection strings into App.config.

Anton Gogolev
No, not app.config. Web.config is correct. This is true even if the entity model is in a separate assembly, with its own app.config.
Craig Stuntz
+3  A: 

The way I do it (with a web app) is to have separate versions of the Web.config file for each of Development (web.config), QA (web-qa.config), and Production (web-prod.config) installs. The config files have different connection strings pointing to the corresponding instances of the database. The default connection string points to the SQL server instance on my local machine which isn't available to the network.

As part of the publishing step, I manually delete the web.config file and rename the appropriate QA/Prod configuration to web.config. This hooks up that instance of the app to the correct instance of the database.

tvanfosson
A: 

You should have one entity dataset, and instatiate the entities object using the constructor that takes a ConnectionString, which you can read in from:

WebConfigurationManager.ConnectionStrings["ConnStrName"].ConnectionString;

The ConnectionStrings element also supports the general configSource attribute.

You can therefore have something like:

<connectionStrings configSource="PathToConnectionStrings.config" />

This allows you to have all the connection strings held in a seperate file, that can then have different values on each environment.

Note that unlike the appSettings file attribute, this doesn't do a merge, it completely replaces the section.

Note that in VS2010, you will have the facility to modify the .config based on the build configuration, so could change the values just by setting your build to "Production" say.

Zhaph - Ben Duguid