EF Connection String Not Found in MSTest Unit Test
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-VAI开发者_StackOverflow社区O;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?
Looks like your problem might get fixed by adding fully-qualified namespaces to your EDMX references, as documented here. :-)
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.
精彩评论