在Azure Web App中设置EF连接字符串 [英] Setting EF Connection String in Azure Web App

查看:72
本文介绍了在Azure Web App中设置EF连接字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们有一个ASP .NET(MVC)应用程序,并且正在使用Entity Framework 6连接到我们的数据库. DbContext以标准方式构造,它代表我们加载连接字符串.生成的代码如下:

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)
    {
    }
}

我们也以标准方式在本地web.config中设置连接字符串:

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" />

将应用程序发布到Azure时,我们导航到Azure门户,然后导航到Web App的设置",然后导航到连接字符串"列表.在那里,我们添加了本地使用的EF连接字符串.当我们重新启动并访问该应用程序时,根据选择的连接字符串的类型,会出现运行时错误.

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.

对于Custom类型,我们会收到以下运行时错误:

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

不支持关键字:数据源".

对于SQL ServerSQL Database,我们收到以下运行时错误:

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

不支持关键字:元数据".

这似乎是一个直截了当的故事,所以我们想知道出了什么问题.

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

推荐答案

问题在于转义的引号是&quot;.

The problem is the escaped quotes: &quot;.

web.config中的连接字符串的引号已转义,因为它们已在XML属性中序列化.在Azure门户中输入连接字符串时,应提供原始的未转义字符串.像这样:

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的答案非常适合确认环境是否按您期望的那样进行设置.通过Visual Studio中的向导进行发布时,注意.pubxml文件也很有帮助:它还将尝试填充连接字符串.

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.

这篇关于在Azure Web App中设置EF连接字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆