Setting EF Connection String in Azure Web App

后端 未结 4 1622
误落风尘
误落风尘 2020-12-30 04:10

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

相关标签:
4条回答
  • 2020-12-30 04:18

    The problem is the escaped quotes: ".

    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.

    0 讨论(0)
  • 2020-12-30 04:20

    Just recording my own experiences in addition to answers already recorded here

    This was my final connection string (on mutliple lines for clarity)

    metadata=res://*/Models.mBT.csdl|res://*/Models.mBT.ssdl|res://*/Models.mBT.msl;
    provider=System.Data.SqlClient;
    provider connection string="
      Data Source=tcp:myazureserver.database.windows.net,1433;
      Initial Catalog=databasename;
      User ID=z@myazureserver;
      Password=xyz"
    

    To convert from a "normal" connection string to one that is accepted by EF:

    • The connection string type in the application settings has to be "other" not "SQL Azure"
    • The connection string value automatically replaces anything published in web.config
    • The metadata name Models.mBT.csdl (and the other two) comes from this:

    Firstly, mBT is the name of my .edmx file

    With regards to the Model. bit, see the answer from @leqid here: MetadataException: Unable to load the specified metadata resource

    You can inspect your obj directory and see these three metadata files are in a subfolder called Models, so you need to prepend with Models.

    0 讨论(0)
  • 2020-12-30 04:22

    '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.

    0 讨论(0)
  • 2020-12-30 04:31

    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 "

    0 讨论(0)
提交回复
热议问题