Azure:上下文在 Code First 模式下使用从 EDMX 生成的代码

Azure: The context is being used in Code First mode with code that was generated from an EDMX

我在本地安装了 MVC 应用 运行。更新到 Azure 后,它开始抛出错误:

The context is being used in Code First mode with code that was generated from an EDMX file for either Database First or Model First development. This will not work correctly. To fix this problem do not remove the line of code that throws this exception. If you wish to use Database First or Model First, then make sure that the Entity Framework connection string is included in the app.config or web.config of the start-up project.

我检查了本地web.config和天蓝色web.config之间是否有任何区别。除了凭据,一切都是一样的。上面写着:

<add name="DBEntities" 
     connectionString="metadata=res://*/DBModel.csdl|res://*/DBModel.ssdl|res://*/DBModel.msl;
                       provider=System.Data.SqlClient;
                       provider connection string=&quot;data source=xx;initial catalog=xx;persist security info=True;user id=xx;password=xx;MultipleActiveResultSets=True;application name=EntityFramework&quot;" 
     providerName="System.Data.EntityClient" />

我正在使用 EF 6.1.3、MVC5

我 运行 进入相同的错误消息并设法在 this post here on Whosebug 的帮助下解决它。

在 Azure Management / Web Apps / [Your web app] / CONFIGURE / connection strings 中,确保三件事:

  1. 连接字符串与您项目中的连接字符串同名。

  2. 连接字符串值包含项目中连接字符串中显示的所有元数据。我的看起来像这样:

    metadata=res://\*/Models.[MyModel].csdl|res://\*/Models.[MyModel].ssdl|res://\*/Models.[MyModel].msl;provider=System.Data.SqlClient;provider connection string="Server=tcp:[myDBServer].database.windows.net,1433;Database=[myDB];User ID=[myDBUser]@[myDBServer];Password=[myPassword];Trusted_Connection=False;Encrypt=True;Connection Timeout=30;"

  3. 第三列(默认设置为SQL数据库)设置为自定义

找到了比在 Azure 连接字符串中添加所有内容更简单的解决方案。我刚刚更改了 Azure 中的连接字符串名称。成功了..

我不得不处理同样的问题。解决方案是

  1. 通过 FTP
  2. 连接到网站
  3. 编辑web.config
  4. 添加以下连接字符串:

    添加名称="NewDatabaseEntities" connectionString="metadata=res:///NewDatabase.csdl|res:///NewDatabase.ssdl|res://*/NewDatabase.msl;provider=System.Data.SqlClient;provider connection string="Data Source=tcp:your.database.windows.net,1433;initial catalog=your;integrated security=False;User Id=your;Password=your;MultipleActiveResultSets=True;App=EntityFramework"" providerName="System.Data.EntityClient"

感谢 Microsoft 对 Azure 上 EntityFramework 数据库优先的糟糕记录。

您需要更改此行metadata=res:///NewDatabase.csdl|res:///NewDatabase.ssdl|res://*/NewDatabase.msl;

metadata=res://*/;