Sql 实体框架在本地工作,但不在azure上工作

Sql 实体框架在本地工作,但不在azure上工作,sql,entity-framework-4,azure,Sql,Entity Framework 4,Azure,我有一个网络项目,它可以在本地完美地工作。 但是,当我将Azure上发布的网站中的连接字符串更改为连接到SQL Azure上的数据库时,它将开始出现此错误 System.Data.Entity.Infrastructure.UnintentionalCodeFirstException: Code generated using the T4 templates for Database First and Model First development may not work correct

我有一个网络项目,它可以在本地完美地工作。 但是,当我将Azure上发布的网站中的连接字符串更改为连接到SQL Azure上的数据库时,它将开始出现此错误

System.Data.Entity.Infrastructure.UnintentionalCodeFirstException: 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.
   at MyClass.OnModelCreating(DbModelBuilder modelBuilder) in c:\a\src\MyProject\Model.Context.cs:line 25
   at System.Data.Entity.Internal.LazyInternalContext.CreateModelBuilder()
   at System.Data.Entity.Internal.LazyInternalContext.CreateModel(LazyInternalContext internalContext)
   at System.Data.Entity.Internal.RetryLazy`2.GetValue(TInput input)
   at System.Data.Entity.Internal.LazyInternalContext.InitializeContext()
   at System.Data.Entity.Internal.InternalContext.GetEntitySetAndBaseTypeForType(Type entityType)
   at System.Data.Entity.Internal.Linq.InternalSet`1.Initialize()
   at System.Data.Entity.Internal.Linq.InternalSet`1.get_InternalContext()
   at System.Data.Entity.Infrastructure.DbQuery`1.System.Linq.IQueryable.get_Provider()
   at System.Linq.Queryable.Select[TSource,TResult](IQueryable`1 source, Expression`1 selector)
我的配置有:

<connectionStrings>
    <add name="MyDBEntities" connectionString="metadata=res://*/MyModel.csdl|res://*/MyModel.ssdl|res://*/MyModel.msl;provider=System.Data.SqlClient;provider connection string=&quot;Server=tcp:[Removed].database.windows.net,1433;Database=MyDB;User ID=[Removed];Password=[Removed];Trusted_Connection=False;Encrypt=True;Connection Timeout=30;&quot;" providerName="System.Data.EntityClient" /> 
    <add name="MyDB" connectionString="metadata=res://*/Model.csdl|res://*/Model.ssdl|res://*/Model.msl;provider=System.Data.SqlClient;provider connection string=&quot;Server=tcp:[Removed].database.windows.net,1433;Database=MyDB;User ID=[Removed];Password=[Removed];Trusted_Connection=False;Encrypt=True;Connection Timeout=30;&quot;" providerName="System.Data.EntityClient" />
</connectionStrings>

我使用连接字符串在本地使用单元测试进行测试,它可以从本地计算机连接到SQLAzure数据库。
感谢您的帮助。

我今天遇到了这个问题;这是我第一次部署到Azure。我一直在拔掉头发,只是我一根头发都没有了我终于找到了答案,这可能与原始海报的问题相同。

与原始海报一样,我在以下配置中进行了测试:

  • 从Visual Studio针对本地数据库运行WCF Web应用程序--成功
  • 将WCF Web应用从Visual Studio部署到本地IIS,在本地DB上运行--成功
  • 从Visual Studio针对Azure SQL DB运行WCF Web应用程序--成功
  • 通过Visual Studio将WCF应用程序部署到Azure,运行于Azure SQL DB——失败
在读了另一篇文章()之后,我得到了一个提示。这篇文章说,如果“连接字符串包含元数据,EF认为它是模型优先或数据库优先”,但如果它是“普通连接字符串,EF认为它是代码优先”。我浏览了部署的Azure网站的Web.config,并确认连接字符串具有对模型优先元数据的正确引用。那么问题出在哪里

我想,也许Azure网站没有读取web.config的连接字符串。回想我是如何创建Azure网站的,我记得我给了Azure SQL DB一个别名,该别名与我在Web.config中的连接字符串“label”的名称完全相同澄清:

  • 在Azure管理控制台中,我查看了网站设置,并查看了我的Azure网站“烘焙”的“连接字符串”设置,作为使用DB创建网站的副作用——连接字符串“handle”是“ssncustinfo modelcontainer”-->我错误地为连接字符串指定了与web.config“handle”相同的“handle”/“alias”,认为这会有所帮助。相反,当EF查找连接字符串时,它找到了这个“别名”句柄,它是一个不包含元数据的“普通”SQL连接字符串。此“别名”屏蔽了web.config中指定的实际连接字符串。

所以我销毁了我的Azure SQL数据库和我的Azure网站。然后,我重新创建了Azure网站,但这次我要求为与相关Azure SQL Server的连接提供连接字符串“alias”,即“SsnCustInfoModelContainer\u Proto”。从本地SQL Server Management Studio初始化Azure SQL DB后,我再次将WCF web应用程序部署到Azure网站(当然,我必须下载新的部署配置文件才能执行此操作),然后再次尝试该应用程序。这次它起作用了--“别名”ssncustinfo modelcontainer_Proto与EF没有冲突,EF也没有找到它。EF转而在web.config中找到了真正的连接字符串以及所有正确的元数据。
问题已解决。

您是先使用代码、先使用模型还是先使用数据库?另外,您的连接字符串看起来如何(您可能会忽略凭据)?您的连接字符串表示您正在以某种方式使用模型(模型优先或数据库优先)-您是否也以某种方式首先使用代码?我发现了这篇关于如何在模型方法中意外地使用代码优先的博文:我不是在使用代码优先。我先有了数据库,然后用它来创建模型。后来,我不得不对模型进行一些更改并更新数据库。我用reflector检查了DAL dll,它的资源中有csdl、msl和ssdl。你为我节省了一些非常宝贵的时间。。。再次感谢。@Nathan Watson,我已经尝试了和你解释的相同的解决方案。但它仍然不起作用。我已经为EF使用了POCO模板,所以我的上下文文件在业务层proj中,tt文件在eninity项目中,而dbmx在另一个项目中。我已经在主MVC项目的web.config中配置了azure连接字符串,它在本地运行得非常完美,但当我在azure上发布时,它并没有连接azure db。您可以参考我发布的问题了解更多详情。