在本地而不是实体框架的工作在Azure上 [英] Entity framework work locally but not on azure

查看:291
本文介绍了在本地而不是实体框架的工作在Azure上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Web项目,该项目完全在本地工作。
但是,当我改变Azure上我发表的网站连接字符串连接到我的SQL Azure数据库将开始给这个错误。

I have a web project which works perfectly locally. But when I change the connection string in my published web site on Azure to connect to my database on SQL Azure it will start giving this error.

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>

我测试使用本地我的单元测试与该连接字符串,并将它从我的本地机器连接到SQL Azure数据库的作品。
任何帮助AP preciated。

I tested using my unit test locally with that connection string and it works from my local machine connecting to SQL Azure database. Any help appreciated.

推荐答案

我今天有这个确切的问题;这是我第一次部署到Azure上。我一直在拉我的头发,除了我没有任何离开。 我终于想通了,并在这里可能是同样的问题,原来的海报是有。

I was having this exact problem today; it's my first time deploying to Azure. I've been pulling my hair out, except I don't have any left. I finally figured it out, and it's probably the same issue original poster here is having.

就像原始的海报,我在这些配置进行测试:

Just like the original poster, I tested in these configurations:


  • 从Visual Studio中对本地数据库运行WCF Web应用程序 - 成功

  • WCF部署Web应用程序从Visual Studio到本地IIS,撞上了本地数据库 - 成功

  • 从Visual Studio中对Azure的SQL数据库运行WCF Web应用程序 - 成功

  • 部署WCF应用程序通过Visual Studio中的Azure,天青对SQL数据库上运行 - 失败!

  • ran WCF Web App from Visual Studio against local DB -- success
  • deployed WCF Web App from Visual Studio to local IIS, ran against local DB -- success
  • ran WCF Web App from Visual Studio against Azure SQL DB -- success
  • deployed WCF App to Azure via Visual Studio, running against Azure SQL DB -- FAILURE!!

阅读另一篇文章后( code首先与数据库首页)我有一个提示。这后说,如果连接字符串的元数据,EF认为这是一型或数据库第一,但如果这是一个普通的连接字符串,EF认为这是code第一。我浏览了部署Azure的Web站点的web.config并确认连接字符串不得不将模型第一的元数据的正确引用。那么,什么是问题?

After reading another post (Code First vs. Database First) I got a hint. That post says that if "connection string has the metadata, EF thinks it is Model First or Database First" but if it's a "plain connection string, EF thinks it is Code First." I browsed the deployed Azure Web Site's web.config and confirmed that the connection string had the proper references to the Model-First metadata. So what was the problem???

我想通或许在Azure网站不读web.config中的连接字符串。回想着我怎么会创造​​在Azure网站,我记得的我放弃了Azure的SQL数据库具有完全相同的名称的别名在web.config !! 澄清:

I figured that perhaps the Azure Website wasn't reading the web.config's connection string. Thinking back to how I'd created the Azure Web Site, I remembered that I'd given the Azure SQL DB an alias with the exact same name as my connection string's 'label' in the web.config!! To clarify:


  • 在Azure的管理控制台我去网站设置和审核,在出炉的我的Azure网站,创建-网站与-DB的副作用的连接字符串设置 - 连接字符串句柄是SsnCustInfoModelContainer - 我会给出错误的连接相同的手柄/别名'作为我的web.config连接字符串句柄,认为这将有助于。相反,当EF查找连接字符串,有人发现这个别名手柄,这是不包含元数据的普通SQL连接字符串。这种别名掩盖在web.config中指定的真正的连接字符串。

  • in the Azure admin console I went to the Website settings and reviewed the "connection string" settings "baked in" to my Azure web site as a side-effect of creating-website-with-DB -- connection string 'handle' was "SsnCustInfoModelContainer" -- I'd mistakenly given the connection the same 'handle'/'alias' as my web.config 'handle' for the connection string, thinking this would help. Instead, when EF looks for the connection string, it was finding this 'aliased' handle, which was a "plain" SQL connection string containing no metadata. This 'alias' masked the real connection string specified in the web.config.

所以我毁了我的Azure SQL数据库和我的Azure网站。然后我重新在Azure网站,但是这一次,我问的连接字符串SsnCustInfoModelContainer_Proto为连接到相关的Azure的SQL Server的别名。从我的本地SQL Server Management Studio中初始化Azure的SQL数据库后,我再部署WCF的Web应用程序到Azure的网站(我必须下载一个新的部署配置文件,当然,要做到这一点),我再次尝试应用。这一次,它的工作 - 的'别名'SsnCustInfoModelContainer_Proto没有抵触,也不会被发现EF。 EF而是继续找到真正的连接字符串,与所有适当的元数据,在web.config。问题解决了。

So I destroyed my Azure SQL DB and my Azure Web Site. Then I recreated the Azure Web Site, but this time I asked for the connection string 'alias' of "SsnCustInfoModelContainer_Proto" for the connection to the associated Azure SQL Server. After initializing the Azure SQL DB from my local SQL Server Management Studio, I deployed the WCF web app again to the Azure Web Site (I had to download a new deployment profile, of course, to do this), I tried the app again. This time it worked -- the 'alias' "SsnCustInfoModelContainer_Proto" did not conflict with and was not found by EF. EF instead went on to find the true connection string, with all the proper metadata, in the web.config. Problem solved.

这篇关于在本地而不是实体框架的工作在Azure上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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