3
votes

I am developing an ASP.NET MVC 4 / .NET 4.0 Web application in Visual Studio 2012 RC, that is meant to run on Windows Azure, utilizing a SQL Azure database. The solution is source controlled by Team Foundation Service, and I have set up the latter to deploy automatically to Azure whenever I check in.

What I'm wondering is, how do I transform my database connection strings (in Web.config) so that they refer to the production SQL Azure server when the project is deployed on Azure? On my development box I make use of a local SQL Express instance.

I'm open to other methods of deployment (than the TFS automatic service), if there are better ways of doing this.

EDIT: I'm connecting to the database via Entity Framework 5.0.0 RC.

3

3 Answers

3
votes

You could also use multiple ServiceConfigurations:

enter image description here

Then, instead of saving your connection string in the web.config you add it as a setting in your ServiceConfiguration:

enter image description here

Since you have multiple configurations you can give a value to SomeConnectionString for each configuration (meaning you can fill in your database settings for the Test, Development and Cloud profiles).

Finally in your code it would be easier to have a factory class that creates the new context based on the connection string in your ServiceConfiguration:

public static class MyDataContextFactory
{
    public static MyDataContextEntities Create()
    {
        return new MyDataContextEntities(RoleEnvironment.GetConfigurationSettingValue("SomeConnectionString"));
    }
}
0
votes

I think you would create multiple web.config files for the different environments

How to: Transform Web.config When Deploying a Web Application Project

There is a blog article listed here on how to combine this in TFS auto deployment

0
votes

If it's just you (or all developers have the same connection string), and you don't mind your connection info in source control, it's easy. Azure will overwrite your connection string with the connection string information that you enter in the Azure Management Portal in the website section under config. Just make sure that the name is the same.

So, for example, if your web.config has name="DefaultConnection" connectionstring="your local db, trusted connection, etc, etc".....as long as your connection string info in Azure has the name "DefaultConnection" (I assume it's pointing to an Azure Database), it'll work.