Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET MVC 4 - How do I transform my database connection strings upon deployment via Team Foundation Service to Azure?

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.

like image 728
aknuds1 Avatar asked Dec 14 '25 16:12

aknuds1


1 Answers

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"));
    }
}
like image 64
Sandrino Di Mattia Avatar answered Dec 16 '25 07:12

Sandrino Di Mattia



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!