我正在尝试构建一个涉及WCF服务的解决方案,该服务调用一个包含EntityFramework6模型的dll。当我尝试单元测试服务然而,我收到一条消息: Additional information: No connection string named 'SyslogEntities' could be found in the application config file.
EntityFramework6使用WCF服务的配置boundrary
我的流被逻辑地布置为:
SyslogDataSvcTest.dll (app.config has service bindings) ->
SyslogDataSvc.svc (web.config has provider and connection string) ->
LibSyslogData.dll (app.config has providers and connection string)
所有触及EF是在libSyslogData.dll的代码。它将数据映射到上层对象内部,并返回它们,而不是公开自己的模型信息,因此EF使用应该是真正的孤立。
那么我做错了什么?
编辑: 我得到它运作良好,但也许是一种奇怪的方式。看来我的app.config中的设置没有在安装EF和MySql依赖项时由NuGet正确指定。
相反,我创建了一个代码中配置类作为这里MSDN描述: https://msdn.microsoft.com/en-us/data/jj680699
现在,我有这个类:
public class EFConfiguration : DbConfiguration {
public EFConfiguration()
{
//For some reason this works much better than the app.config.
//With this defined, upper layer config documents need only specify the "SyslogEntities" Connection string.
SetExecutionStrategy("MySql.Data.MySqlClient",() => new MySqlExecutionStrategy());
SetDefaultConnectionFactory(new MySqlConnectionFactory());
SetProviderFactory("MySql.Data.MySqlClient", new MySqlClientFactory());
SetProviderServices("MySql.Data.MySqlClient", new MySqlProviderServices());
}
}
我可以离开DB实现的细节到数据层,并且只在上层配置连接字符串。
谢谢!我正在调试一个解决方案中的所有东西(IIS 8 Express for debug),所以现在回想起来,单元测试可能会跳过服务托管,然后直接按照您的建议直接访问dll。我通过添加连接字符串(我尝试过在发布之前无济于事),而且还将实体框架提供程序,提供程序工厂和连接工厂定义添加到单元测试app.config中,从而得到它的工作。我有点惊讶,因为它包含Mysql库的引用,但它并没有让我直接引用dll。 –
奇怪的是,它看起来像一个基于代码的定义效果更好。在将配置移动到单元测试类之前,我创建了一个配置类,并且一切正常,但是当我删除配置类时,我开始出现提供程序定义错误。我认为在通过NuGet安装EF和MySql时,配置代码不正确。谢谢你的帮助。 –