突然,在实例化生成的ObjectContext类时,我一直得到一个MetadataException。App.Config中的连接字符串看起来是正确的-自从上次它工作以来没有改变-我已经尝试从底层数据库中重新生成一个没有变化的新模型(edmx-file)。
有人有什么想法吗?
进一步的细节:我没有更改任何属性,没有更改任何输出程序集的名称,也没有尝试将EDMX嵌入到程序集中。我只是下班后等了10个小时才回来。然后它就不管用了。
我试过重新创造EDMX。我试着重新创建这个项目。我甚至尝试从头开始重新创建数据库。不管怎样,运气不好。
在我的情况下,列出的答案没有一个是有效的,所以我发布了这个。
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
当我得到元数据问题排序,我有一个后续的问题,形式是调用异常无法找到app.config中XXXEntities的连接字符串(其中我的目标是不依赖app.config)。幸运的是,我找到了引用系统。我的单元测试项目中的数据清除了最后的障碍。总结一下:
使用nuget将实体框架安装到单元测试项目中。
确保System. data . entity和System. data . entity。引用数据。
按照这里的描述对连接字符串进行排序。
将连接字符串传递给分部类构造函数。
现在我的元数据在一个类库中,这个类库可以从引用db更新,并且我可以在运行时将我的应用程序和单元测试指向任何服务器上的任何db。
附录:当我移动我的edmx到一个文件夹,我又得到了错误。经过一些研究,我发现您希望元数据字符串看起来像:metadata=res://EPM.DAL/Models.EPM。其中EPM. csdl。DAL是组装和EPM的名称。Edmx在models文件夹中。