当前位置: 首页 > 面试题库 >

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

袁雅逸
2023-03-14
问题内容

我有一个在本地完美运行的Web项目。但是,当我在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>

我使用该连接字符串在本地使用单元测试进行了测试,并且该测试可以从连接到SQL Azure数据库的本地计算机上运行。任何帮助表示赞赏。


问题答案:

我今天遇到了这个确切的问题;这是我第一次部署到Azure。我一直在拔头发,除了我没剩了。 我终于想通了,这可能与原始海报的问题相同。

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

  • 针对本地数据库从Visual Studio运行了WCF Web应用程序-成功
  • 从Visual Studio到本地IIS部署了WCF Web应用程序,并针对本地数据库运行-成功
  • 针对Azure SQL DB从Visual Studio运行了WCF Web应用程序-成功
  • 通过Visual Studio将WCF应用程序部署到Azure,并针对Azure SQL DB运行-失败!

我发现 也许Azure网站没有读取web.config的连接字符串 。回想一下如何创建Azure网站,我记得 我给Azure SQL
DB赋予了一个别名,其别名与web.config中连接字符串的“标签”的名称完全相同!
澄清:

  • 在Azure管理控制台中,我转到“网站”设置,并查看了“嵌入”到我的Azure网站中的“连接字符串”设置,这是create-website-with-DB的副作用-连接字符串“句柄”为“ SsnCustInfoModelContainer” – 我错误地为连接字符串赋予了与我的web.config“ handle”相同的“ handle” /“ alias”作为连接字符串,认为这样做会有所帮助。 取而代之的是,当EF查找连接字符串时,它发现了这个“别名”句柄,它是一个不包含元数据的“普通” SQL连接字符串。此“别名”掩盖了web.config中指定的实际连接字符串。

因此,我销毁了我的Azure SQL DB和我的Azure网站。然后,我重新创建了Azure网站,但是这次,我要求输入“
SsnCustInfoModelContainer_Proto”的连接字符串“ alias”以连接到关联的Azure SQL Server。从本地SQL
Server Management Studio初始化Azure SQL DB之后,我再次将WCF
Web应用程序部署到Azure网站(当然,我必须下载新的部署配置文件),然后再次尝试了该应用程序。这次它起作用了- “别名”“
SsnCustInfoModelContainer_Proto”与EF没有冲突,也没有被EF找到。
相反,EF继续在web.config中找到包含所有正确元数据的真实连接字符串。
问题解决了。



 类似资料:
  • 我有一个在Azure中运行的Web应用程序。我设置了实体框架来访问我的数据库。“编辑”和“删除”视图在我的本地计算机上以及部署到临时插槽时工作。当我推到生产槽时,我得到一个错误。出于某种原因,控制器没有读取记录ID,即使它显然是在URL中传递的,并且是在RouteConfig中设置的。 更新:这是请求。https://teamweb.azurewebsites.net/JobTracking/Ed

  • 我正在使用PHP*TCPDF/FPDI类动态生成PDF。。 虽然本地发展(WAMP/localhost)... 现在我已经把东西上传到了直播服务器上。。我在同一个文件上只得到一张空白页。。使用相同的目录结构。。 但是,在实时环境中进行测试时。。它不起作用? 只是一个空白页,没有错误...等等。 这是我目前使用的代码。。 (我试图在构造函数和超文本标记语言输出中添加UTF-8编码...?? 代码:

  • IIS可能会搞砸它?我倾向于认为IIS对于和)有不同的进程,因此,我在app.post上引用的套接字与我在()中加入用户是不同的。 在Azure/IIS中使用socket.io房间时有什么提示吗?谢了!

  • 我已经安装了Wamp Server 2.4。安装正常。我可以登录到本地主机。除了phpmyadmin页面之外,一切都正常(包括sqlbuddy和webgrind)。你能帮我解决可能导致这个问题的原因吗。我在这里附上apache访问日志: 127 . 0 . 0 . 1---[2013年7月17日:17:20:43 0530]" GET/phpmyadmin/HTTP/1.1 " 500-127 .

  • 问题内容: 我已经进行了永久搜索,无法为我的问题提供确切的答案。就是这样。我有一个看起来像这样的JSON文件(我去了jsonlint进行验证,它说的很好): 我使用jQuery解析并使用此函数放置在html页面上: 它完美地工作!现在是我的问题,JSON文件将不在本地托管,并且实际上将托管在单独的域中。所以我修改了我的代码如下(经过阅读),希望它能正常工作: 通过添加“回调”行,我停止出现“无法加

  • 我正在使用Amazon Linux AMI在EC2 AWS上运行以下脚本 从今天起,由于EC2上没有任何更改或新安装,脚本在昨天之前一直工作时停止工作。 本地计算机上的相同脚本仍然有效。 相反,在EC2上会出现以下错误: (节点:12636)UnhandledPromiseRejectionWarning:错误:导航失败,因为浏览器已断开连接!在cdpsession.lifecyclewatche