3

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 Answers 3

3

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

4 Comments

This answer is specific to a Web service project, right? I'm developing an ASP.NET MVC Web app. Not sure how this translates to the MVC app scenario.
No, this applies to any type of project (WebRole with WCF, MVC, ... and even WorkerRoles).
1. How do you bring up this configuration UI for your service configuration? I can only see how to edit it textually. 2. What do I gain by defining connection strings in cloud configuration files as opposed to Web.config transforms?
Answer to #1: In the Solution Explorer, in the Cloud Service project (typically has the same name as the solution), expand the Roles node, then right-click on a particular role and select Properties. On the Properties page, select Settings.
0

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

6 Comments

You mean I should create a Web.config transformation for the production environment, no? But how would I make the right transformation be applied during TFS auto-deployment?
i think this blog article explains it softscenario.blogspot.sg/2011/09/…
If you think the article contains a working solution, I suggest you edit your answer to detail that particular solution :)
If you edit your answer so that it details a working solution I may accept it.
Just linking to blog posts is too vague. I'll see if I can come up with something that works, I found an EF blog post that is quite specific in what you need to do.
|
0

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.

1 Comment

I was not able to get this to work. I have two connection strings in my web.config file which are used for my development database. I added the production database connection strings to the Cloud ServiceConfiguration file. I also checked to make sure the connection strings for production were in the Azure Management portal after I deployed, and they were. But Azure does not use them! I made sure there were no typos. Did you have to do anything else in order to get this to work? @SumOfDavid

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.