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

有人有什么想法吗?

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

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


当前回答

我在解决方案文件夹中包含项目的解决方案中遇到了同样的问题,当它们被移动到解决方案根目录时(为了克服Mvc3AppConverter由于项目位置而导致的疑似bug)。

尽管在需要时重新添加了所有*项目引用后编译了解决方案,但在网站启动时抛出了错误。

EDMX在其中一个被移动的项目(“Data”项目)中,但当然,缺少对Data项目的引用并不会导致编译错误,只是运行时错误。

简单地将缺少的引用添加到主项目就解决了这个问题,根本不需要编辑连接。

我希望这能帮助到其他人。

其他回答

我也遇到了和Rick一样的问题和解决方案,只是我要将一个现有的.edmx导入到一个新项目中,虽然基本名称空间无关紧要,但它被导入到不同的子目录中,因此我还必须更新Web内部的连接字符串。配置在三个地方,包括不同的子目录命名:

和一个快速的方法来检查模型名称没有Reflector....查找目录

...obj - {} - edmxResourcesToEmbed输出配置

检查.csdl、.msl和.ssdl资源文件是否存在。如果它们在子目录中,则子目录的名称必须加在模型名称之前。

例如,我的三个资源文件在一个子目录Data中,所以我的连接字符串必须是

元= res: / / * Data.MyModel.csdl | res: / * / Data.MyModel.ssdl | res: / * / Data.MyModel.msl;

(与元= res: / / * / MyModel csdl | res: / / * / MyModel ssdl | res: / / * / MyModel msl;)。

我也有这个问题,这是因为我的网络中的连接字符串。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。解决了配置和问题。:)

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

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

我从解决方案中的所有项目中删除了\bin和\obj文件夹,然后重新构建解决方案,它工作得很好