Set the application ConnectionString in the Service Configuration instead of web.config in Azure Set the application ConnectionString in the Service Configuration instead of web.config in Azure azure azure

Set the application ConnectionString in the Service Configuration instead of web.config in Azure


In your ServiceConfiguration.cscfg file add:

<ServiceConfiguration ... />  <Role ... />    <ConfigurationSettings>      <Setting name="DatabaseConnectionString" value="put your connection string here" />    </ConfigurationSettings>  </Role></ServiceConfiguration>

Now you have a connection string you can change by editing the configuration inside the azure portal.

Then anytime you need to retrieve the connection string you can do it using:

using Microsoft.WindowsAzure.ServiceRuntime;...String connString = RoleEnvironment.GetConfigurationSettingValue("DatabaseConnectionString")

You may need to add Microsoft.WindowsAzure.ServiceRuntime.dll to your references.

RoleEnviroment.IsAvailable can be used to test if your are running in Azure, and if not to fall back to your web.config settings.

using System.Configuration;using Microsoft.WindowsAzure.ServiceRuntime;...if (RoleEnvironment.IsAvailable){    return RoleEnvironment.GetConfigurationSettingValue("DatabaseConnectionString");}else{    return ConfigurationManager.ConnectionStrings["DatabaseConnectionString"].ConnectionString; }

This article has a more verbose explanation of the above.


For Entity Framework, you do not need to provide a providerName, it's already inside in the connectionstring. The reason why it does not work when it's in azure settings is, it contains &quot symbol which needs to be transalated back to " before creating a new EntityConnection. You can do it using HttpUtility.HtmlDecode in System.Web.


Basically you need to define these setting in Azure service configuration file. Check here. Once defined these can be changed from Azure portal.