Setting EF Connection String in Azure Web App(在 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="...""
providerName="System.Data.EntityClient" />
当我们将应用程序发布到 Azure 时,我们导航到 Azure 门户,然后导航到 Web 应用程序的设置,然后导航到连接字符串列表.在那里,我们添加了我们在本地使用的 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 Server
或 SQL 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.
推荐答案
问题是转义引号:"
.
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 连接字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:在 Azure Web App 中设置 EF 连接字符串
基础教程推荐
- rabbitmq 的 REST API 2022-01-01
- SSE 浮点算术是否可重现? 2022-01-01
- 有没有办法忽略 2GB 文件上传的 maxRequestLength 限制? 2022-01-01
- MS Visual Studio .NET 的替代品 2022-01-01
- 如何在 IDE 中获取 Xamarin Studio C# 输出? 2022-01-01
- 将 Office 安装到 Windows 容器 (servercore:ltsc2019) 失败,错误代码为 17002 2022-01-01
- c# Math.Sqrt 实现 2022-01-01
- 为什么Flurl.Http DownloadFileAsync/Http客户端GetAsync需要 2022-09-30
- 如何激活MC67中的红灯 2022-01-01
- 将 XML 转换为通用列表 2022-01-01