MSTest 单元测试中未找到 EF 连接字符串

发布于 2024-10-10 03:08:09 字数 696 浏览 0 评论 0原文

我正在使用 EF4 构建一个新应用程序。 EF 代码与存储库代码一起位于其自己的程序集中。我正在尝试使用 Visual Studio 的 MSTest 进行一些单元测试,但开始时遇到困难。我的代码是 -

string s = "metadata=res://*/DataModels.MonitorUrlEf.csdl|res://*/DataModels.MonitorUrlEf.ssdl|res://*/DataModels.MonitorUrlEf.msl;provider=System.Data.SqlClient;provider connection string=\"Data Source=TOM-VAIO;Initial Catalog=WebMonDb4;Integrated Security=True;Pooling=False;MultipleActiveResultSets=True\"";  
var DbContext = new WebMonDb4Entities(s);

WebMonDb4Entities 是 EF 设计者创建的 EF 上下文。我将连接字符串放入变量中,这样我就不必在测试期间处理连接文件所在位置的问题。该字符串的内容是从 EF 设计器代码所在项目中的 app.config 文件复制的。

错误是“无法加载指定的元数据资源。”我已经做了一些研究,这应该可行。有什么建议吗?

I'm building a new application with EF4. The EF code is in it's own assembly along with the repository code. I'm trying to do some unit testing with Visual Studio's MSTest, but I'm have trouble getting started. My code is -

string s = "metadata=res://*/DataModels.MonitorUrlEf.csdl|res://*/DataModels.MonitorUrlEf.ssdl|res://*/DataModels.MonitorUrlEf.msl;provider=System.Data.SqlClient;provider connection string=\"Data Source=TOM-VAIO;Initial Catalog=WebMonDb4;Integrated Security=True;Pooling=False;MultipleActiveResultSets=True\"";  
var DbContext = new WebMonDb4Entities(s);

WebMonDb4Entities is the EF Context created by the EF designer. I'm putting the connection string in a variable so I don't have to deal with the issues of where the connection files are during the testing. The contents of the string are copied from the app.config file in the project where the EF designer code is.

The error is "Unable to load the specified metadata resource." I've done some research and this should work. Any suggestions?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

窝囊感情。 2024-10-17 03:08:09

看起来您的问题可能会通过向 EDMX 引用添加完全限定的命名空间来解决,如文档所述 此处。 :-)

Looks like your problem might get fixed by adding fully-qualified namespaces to your EDMX references, as documented here. :-)

雨落□心尘 2024-10-17 03:08:09

您可以添加/复制单元测试项目的工作配置文件。或者在其中复制/创建连接字符串元素。

从一些研究中我发现最好创建 EntityConnection 并使用它。

另请查看一些讨论 这里是同一主题。

You can add/copy your working config file for unit test project. Or copy/create your connection string element in it.

And from some research I find that better to create instance of EntityConnection and use it.

Also check some discussion here on same topic.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文