突然,在实例化生成的ObjectContext类时,我一直得到一个MetadataException。App.Config中的连接字符串看起来是正确的-自从上次它工作以来没有改变-我已经尝试从底层数据库中重新生成一个没有变化的新模型(edmx-file)。

有人有什么想法吗?

进一步的细节:我没有更改任何属性,没有更改任何输出程序集的名称,也没有尝试将EDMX嵌入到程序集中。我只是下班后等了10个小时才回来。然后它就不管用了。

我试过重新创造EDMX。我试着重新创建这个项目。我甚至尝试从头开始重新创建数据库。不管怎样,运气不好。


当前回答

我昨天遇到了这个问题,并在调试中查看了我的代码和SQL分析器的输出。

在我阅读和理解这篇文章之前,我不能理解的是,为什么EntityFramework在调用DB时抛出这个错误。我在SQL Profiler中查看了数百行,试图找出数据库模型的错误。我找不到我期待的那种电话,老实说,我也不确定我在找什么。

如果您处于此位置,请检查连接字符串。我的猜测是,在EntityFramework创建它的SQL之前,它会检查模型,在连接字符串的元数据部分指定。对我来说,这是错误的。EntityFramework甚至还没有到DB。

确保名字是正确的。一旦我得到了整理,我然后看到调用SQL Profiler的ApplicationName是'EntityFramework'与SQL调用预期的表。

其他回答

我刚花了30分钟玩这个。我重命名了实体对象,重命名了配置文件中的条目,但还有更多…您还必须更改对CSDL的引用

很容易错过-如果您正在重命名,请确保您获得所有内容....

我也遇到了类似的问题。我的类名与我的文件名不同。生成的connectionstring中包含类名而不是文件名。对我来说,解决方案就是重命名我的文件以匹配类名。

在我的情况下,列出的答案没有一个是有效的,所以我发布了这个。

For my case, building on Visual studio and running it with IIS express worked fine. But when I was deploying using Nant scripts as a stand-alone website was giving errors. I tried all the suggestions above and then realized the DLL that was generated by the nant script was much smaller than the one generated by VS. And then I realized that Nant was unable to find the .csdl, .msl and .ssdl files. So then there are really two ways to solve this issue, one is to copy the needed files after visual studio generates them and include these files in the build deployment. And then in Web.config, specify path as:

"metadata=~/bin/MyDbContext.csdl|~/bin/MyDbContext.ssdl|~/bin/MyDbContext.msl;provider=System.Data.SqlClient;...."

这是假设您已经手动复制文件到您正在运行的网站的bin目录。如果它在不同的目录中,则相应地修改path。 第二种方法是在Nant脚本中执行EdmGen.exe,并生成文件,然后将它们作为资源,如下例所示: https://github.com/qwer/budget/blob/master/nant.build

对于所有SelftrackingEntities用户, 如果您已经遵循Microsoft演练并将Object上下文类分离为 WCF服务项目(通过链接到上下文.tt)所以这个答案是给你的:

这篇文章中显示的部分答案包括如下代码:

... = string.Format("res://{0}/YourEdmxFileName.csdl|res://{0}/YourEdmxFileName.ssdl|res://{0}/YourEdmxFileName.msl", 
        typeof(YourObjectContextType).Assembly.FullName); 

对你没用!!原因是youobjectcontexttype。程序集现在驻留在不同的程序集(在wcf项目程序集内),

你应该用——>替换youobjectcontexttype。assembly。fullname

ClassTypeThatResidesInEdmProject.Assembly.FullName 

玩得开心。

我也有这个问题,这是因为我的网络中的连接字符串。config与我的EDMX所在程序集的app.config中的略有不同。不知道为什么会改变,但这里有两个不同的版本。

App.config:

<add name="SCMSEntities" connectionString="metadata=res://*/Model.SMCSModel.csdl|res://*/Model.SMCSModel.ssdl|res://*/Model.SMCSModel.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=SANDIEGO\sql2008;initial catalog=SCMS;integrated security=True;multipleactiveresultsets=True;application name=EntityFramework&quot;" providerName="System.Data.EntityClient" />

. config:

<add name="SCMSEntities" connectionString="metadata=res://*/Model.SCMSModel.csdl|res://*/Model.SCMSModel.ssdl|res://*/Model.SCMSModel.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=SANDIEGO\sql2008;initial catalog=SCMS;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient" />

修复它只是简单地复制App .config字符串(注意结尾的小差异-而不是“App=EntityFramework”,它想要“应用程序名称=EntityFramework”)到web。解决了配置和问题。:)