如何在microsoft azure上配置实体框架(Model First)?

时间:2014-07-24 08:20:49

标签: azure code-first ef-database-first

我在我的应用程序中使用EntityFramwok(代码优先),但由于某些原因,我必须将entityframwork approch更改为Database First。我已经成功地在本地配置了项目,当将代码发布到microsoft 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. If you are creating your own DbConnection, then make sure that it is an EntityConnection and not some other type of DbConnection, and that you pass it to one of the base DbContext constructors that take a DbConnection. To learn more about Code First, Database First, and Model First see the Entity Framework documentation here: http://go.microsoft.com/fwlink/?LinkId=394715" 我在google上搜索过它但找不到任何线索,似乎我是唯一一个得到这个例外的人:(有没有人知道这个。我是azure的新手,所以不知道怎么改变那里的entityframwork方法。任何帮助或建议将不胜感激:)

2 个答案:

答案 0 :(得分:3)

我刚刚将一个数据库第一个项目部署到Azure并遇到了相同的错误。现在谷歌搜索时的信息似乎比你提出这个问题时要多得多。无论如何,对我而言,这一切都不是完全有意义的,我不得不尝试提出解决方案。

这是我的情景: 我的数据库第一个数据库也托管在Azure上,Azure推荐用于ADO.NET的连接字符串是我放在Web.Release.config转换条目中的。我有两个连接字符串,一个是默认连接,另一个是用于db第一个项目的连接字符串。解决方案是使用来自Web.config连接字符串的相同(正常运行)内容,并仅使用Azure数据库连接字符串替换内部部分。我被剪掉了过去很高兴,只是消除了导致错误的连接字符串的全部内容。

连接字符串中的批次信息特定于您的项目以及您在项目中创建的数据库第一个实体。以下是我的几个例子:

你是Web.config应该有本地开发环境的连接字符串(这些是由DB第一个向导为你创建的机会):

    <configuration>
    ...
      <connectionStrings>
    <add name="DefaultConnection" connectionString="Data Source=(LocalDb)\v11.0;AttachDbFilename=|DataDirectory|\aspnet-WebRole1-20150218073037.mdf;Initial Catalog=aspnet-WebRole1-20150218073037;Integrated Security=True" providerName="System.Data.SqlClient" />
    <add name="DbFirstModelEntities" connectionString="metadata=res://*/Models.DbFirstModel.csdl|res://*/Models.DbFirstModel.ssdl|res://*/Models.DbFirstModel.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=(localdb)\ProjectsV12;initial catalog=DbFirstData;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient" />
  </connectionStrings>
...
    </configuration>

注意dbfirst连接字符串是如何从一堆关于db模型的元数据开始的。这个东西很重要!!

接下来是正确的Web.Release.config连接字符串转换(只是发布数据库第一个,但你需要为你拥有的其他任何东西做类似的事情):

<connectionStrings>
  <add name="DbFirstModelEntities"
       connectionString="metadata=res://*/Models.DbFirstModel.csdl|res://*/Models.DbFirstModel.ssdl|res://*/Models.DbFirstModel.msl;provider=System.Data.SqlClient;provider connection string=&quot;Server=tcp:<yourdatabesserver>.database.windows.net,1433;Database=DbFirstData;User ID=<user>;Password=<password>;Trusted_Connection=False;Encrypt=True;Connection Timeout=30;MultipleActiveResultSets=True;App=EntityFramework&quot;"
       providerName="System.Data.EntityClient"
       xdt:Transform="SetAttributes" xdt:Locator="Match(name)"/>
</connectionStrings>

这么长的故事,如果您在发布到azure后遇到此错误:请检查您的转换并确保您没有删除所有重要的元数据!

答案 1 :(得分:2)

在azure服务器上配置网站和数据库时,必须定义连接字符串,连接字符串的名称不应与web.config文件中的连接字符串名称相同。我刚刚在web.config中重命名连接字符串名称并解决了问题。 :)

相关问题