Azure发布数据库第一个连接字符串不工作 [英] Azure Publish Database First Connection String Not Working

查看:156
本文介绍了Azure发布数据库第一个连接字符串不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have gone through the steps to publish my web app using database first on the azure portal.

我已经完成了使用数据库首先发布我的网络应用程序的步骤, p>

However, when I publish I get this error message:


使用数据库优先和模型
的T4模板生成的代码如果在代码优先模式下使用,首次开发可能无法正常工作。
要继续使用Database First或Model First,请确保在
执行应用程序的配置文件中指定了Entity
Framework连接字符串。要使用这些类,从
Database First或Model First生成,使用代码首先使用属性或DbModelBuilder API添加任何额外的
配置,然后
删除引发此异常的代码。

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之后被发布修改:

My connection string in the web.config after it has been modified by publish:

<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生成):

My context (generated by edmx):

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

我很困惑,因为它似乎是实体框架尝试首先使用代码而不是数据库。

I am very confused becuase it seems like entity framework is trying to use code first rather than database first.

更新:
我刚刚尝试在本地和网络上使用相同的连接字符串应用程序似乎运行正常。网络应用程序确实连接到远程数据库。

UPDATE: I just tried using the same connection string locally and the web app seems to run fine. The web app does connect to the remote database fine. It is only when I publish to azure it fails.

推荐答案

请在实体框架在本地工作,但不在天蓝色上

如果你做了同样的错误我做了,这是发生了什么... Azure部署的应用程序没有找到你的连接字符串MySiteEntities在你的web.config中。相反,当您创建Azure网站(或Cloud Service或其他)时,您创建了一个关联的Azure SQL数据库,并将其连接字符串与MySiteEntities完全相同。后一个连接字符串是一个纯连接字符串,没有Model / Database-first元数据引用,所以被EF视为Code-First连接,然后EF就是对冲突的抱怨。请参阅代码优先与数据库优先,以了解此区别的解释。

If you made the same "mistake" I did, this is what's happening ... the Azure-deployed app isn't finding your connection string "MySiteEntities" inside your web.config. Instead, at the time your created your Azure Web Site (or Cloud Service or whatever), you created an associated Azure SQL Database and gave its connection string the exact same name, "MySiteEntities". This latter connection string is a "plain" connection string without Model/Database-first metadata references, and so is being treated as a Code-First connection by EF, which then complains about the conflict. See Code First vs. Database First for an explanation of this distinction.

这篇关于Azure发布数据库第一个连接字符串不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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