Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting EF Connection String in Azure Web App

We have an ASP .NET (MVC) app and are using Entity Framework 6 to connect to our databases. The DbContext is constructed in a standard way and it loads the connection string on our behalf. The generated code looks like this:

public partial class MyContext : DbContext
{
    public MyContext(string connectionName)
        : base("name=" + connectionName)
    {
    }
}

We set the connection string in a local web.config also in a standard way:

<configuration>
  <connectionStrings>
    <add name="DefaultConnection" 
         connectionString="metadata=...;provider connection string=&quot;...&quot;" 
         providerName="System.Data.EntityClient" />

When we publish the app to Azure we navigate to the Azure Portal, then to the Web App's Settings, then to the list of Connection Strings. There we add the EF connection string that we had used locally. When we restart and visit the app we get a run-time error depending on the type of connection string we choose.

For a Custom type we get the following run-time error:

Keyword not supported: 'data source'.

For SQL Server or SQL Database we get the following run-time error:

Keyword not supported: 'metadata'.

This really seems like a straightforward story so we are wondering what is going wrong.

like image 749
Jacob Foshee Avatar asked Jun 30 '15 22:06

Jacob Foshee


3 Answers

The problem is the escaped quotes: &quot;.

The connection strings in web.config have quotes escaped because they are serialized in an XML attribute. When entering a connection string in the Azure portal you should provide the raw unescaped string. Something like this:

metadata=...;provider connection string="Data Source=..."

David Ebbo's answer is good for confirming that the Environment is set up as you expect. It is also helpful to pay attention to the .pubxml file when publishing via the wizard in Visual Studio: it will try to populate connection strings as well.

like image 174
Jacob Foshee Avatar answered Oct 18 '22 03:10

Jacob Foshee


'custom' should be correct here. In that case, the providerName is left unchanged, so if you have System.Data.EntityClient in your config, that should remain after the Azure runtime changes it.

Try going to Kudu Console and click on Environment to make sure the conn string looks correct there.

like image 22
David Ebbo Avatar answered Oct 18 '22 03:10

David Ebbo


If you have this line in web.connfig

<add name="Entities" connectionString="metadata=res://*/TestDB.csdl|res://*/TestDB.ssdl|res://*/TestDB.msl;provider=System.Data.SqlClient;provider connection string=&quot;Data Source=XXXXXXXX.database.windows.net,1433;Initial Catalog=YourDB;User ID=YourUser;Password=XXXXXX&quot;" providerName="System.Data.EntityClient" />

Add this in azure portal:

Name Column => Entities

Value Column => metadata=res://*/TestDB.csdl|res://*/TestDB.ssdl|res://*/TestDB.msl;provider=System.Data.SqlClient;provider connection string="Data Source=XXXXXXXX.database.windows.net,1433;Initial Catalog=YourDB;User ID=YourUser;Password=XXXXXX" 

"Custom" - In the drop selection box

Make sure (as stated in first answer) to replace &quot; with "

like image 7
Maayan Hope Avatar answered Oct 18 '22 01:10

Maayan Hope