Two days later, I finally managed to get it to work. I am adding my solution here, hoping this can help someone in the future.
Environment
- Azure API (but theoretically, it should also work for other types of projects)
- SQL DB (hosted on Azure). Obviously, any db
- EF 6.0 - First Database Approach
- The connection string is stored in Azure under โApplication Settings โ Connection Strings.โ Image shown below

What I wanted to do I wanted to put the connection string on Azure and, as @Sandrino Di Mattia mentioned, dynamically retrieve it from there.
Steps that worked for me
- Create connectionStrings in web.config
`
<connectionStrings> <add name="dbConnectionStringForAzure" connectionString="Local Connection String" providerName="System.Data.EntityClient"/> </connectionStrings>
`
Note that the provider name is System.Data.EntityClient, not System.Data.SqlClient.
Additional bit: In addition, after publishing the project, you can check that the connection string is in the web.config file. Go to projectorapiurl.scm.azurewebsites.net.
Go to Menu -> Debug Console -> PowerShell -> Edit Web.config file. (There are other ways to get web.config files. Use the one you prefer.)
Note. Here you can find another line of generated Azure autorun. It is safe to remove as we are not using it.
Go to Azure -> your project and application settings. Add the data as shown below:
Name = dbConnectionStringForAzure
Value = Provider=System.Data.SqlClient; metadata=res://*/csdlModel.csdl|res://*/ssdlModel.ssdl|res://*/mslModel.msl; Provider Connection String ='Data Source=server.database.windows.net,1433;initial catalog=database;User ID=username;Password=password;MultipleActiveResultSets=True;App=EntityFramework;' Provider=System.Data.SqlClient; metadata=res://*/csdlModel.csdl|res://*/ssdlModel.ssdl|res://*/mslModel.msl; Provider Connection String ='Data Source=server.database.windows.net,1433;initial catalog=database;User ID=username;Password=password;MultipleActiveResultSets=True;App=EntityFramework;'
Before the third drop-down menu, select CUSTOM. It is important that Azure adds System.Data.SqlClient (or any other provider, depending on which one is selected) in the provider name of our connection string, which we do not need.
- Save
At this point, Azure should use this connection string at runtime. Want to check it out!? similar to what @Sandrino Di Mattia suggested above or in this post SO @Shaun Luttin Get the Azure SQL Database Connection String that is linked to the Windows Azure website with C # .NET .
Alternatively, put the code below in any razor template:
<p>Value of dbConnectionStringForAzure : @System.Configuration.ConfigurationManager.ConnectionStrings["dbConnectionStringForAzure"].ConnectionString </p>
On the other hand, I set the connection string name to my DbContext constructor.
public MyEntities() : base("name=dbConnectionStringForAzure") { }
Now that I called my API, it dynamically used the connection stored in Azure.
Thanks to dozens of messages and additional snapshots of coffee!