gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-30 01:21:25 24 4
gpt4 key购买 nike

我已完成首先在 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com