c# - Azure 发布数据库第一个连接字符串不起作用

标签 c# asp.net-mvc entity-framework azure publish

我已完成首先在 Azure 门户上使用数据库发布 Web 应用程序的步骤。

但是,当我发布时,我收到此错误消息:

Code generated using the T4 templates for Database First and Model First development may not work correctly if used in Code First mode. To continue using Database First or Model First ensure that the Entity Framework connection string is specified in the config file of executing application. To use these classes, that were generated from Database First or Model First, with Code First add any additional configuration using attributes or the DbModelBuilder API and then remove the code that throws this exception.

通过发布修改 web.config 中我的连接字符串:

<add name="MySiteEntities" connectionString="metadata=res://*/MySite.csdl|res://*/MySite.ssdl|res://*/MySite.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=tcp:**********.database.windows.net,****;initial catalog=MySite;user id=username@**********;password=*******;MultipleActiveResultSets=True&quot;" providerName="System.Data.EntityClient" />

我的上下文(由 edmx 生成):

public partial class MySiteEntities : DbContext
{
    public MySiteEntities()
        : base("name=MySiteEntities")
    {
    }
...

我很困惑,因为 Entity Framework 似乎试图首先使用代码而不是数据库。

更新: 我只是尝试在本地使用相同的连接字符串,并且网络应用程序似乎运行良好。 Web 应用程序确实可以正常连接到远程数据库。只有当我发布到 azure 时才会失败。

最佳答案

阅读我对类似问题的回答 Entity framework work locally but not on azure .

如果您犯了与我相同的“错误”,这就是正在发生的事情...Azure 部署的应用程序在 web.config 中找不到您的连接字符串“MySiteEntities”。相反,在创建 Azure 网站(或云服务或其他)时,您创建了关联的 Azure SQL 数据库,并为其连接字符串指定了完全相同的名称“MySiteEntities”。后一个连接字符串是一个“普通”连接字符串,没有模型/数据库优先元数据引用,因此被 EF 视为代码优先连接,然后 EF 会提示冲突。请参阅Code First vs. Database First了解这种区别的解释。

关于c# - Azure 发布数据库第一个连接字符串不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15740625/

相关文章:

c# - Hook API 函数 GetSystemMetrics

c# - 从 FileResult 重定向到 MVC ActionResult

c# - 使用 linq 表达式的 params 数组

c# - 避免时钟程序中的溢出错误

c# - 如果花费的时间太长,则取消执行命令

c# - 自定义控件可绑定(bind)属性中的值未更新 - Xamarin forms/Prism

c# - 无法加载类型 'System.IdentityModel.Tokens.JwtSecurityToken'

asp.net - Azure blob 是否需要文件扩展名?

c# - 带有 Entity Framework 的带输出参数 (SYS_REFCURSOR) 的过程

c# - 间歇性System.ComponentModel.Win32Exception : The network path was not found