我的解决方案中有很多测试。测试分布在几个不同的测试项目中。我将Entity Framework 6.3与基于代码的配置一起使用。我的DbConfiguration
类看起来像这样
namespace MySolution.Data
{
public class MyEntityConfiguration : DbConfiguration
{
public MyEntityConfiguration()
{
AddInterceptor(new StringTrimmerInterceptor());
}
}
public class StringTrimmerInterceptor : IDbCommandTreeInterceptor
{
// ... do stuff
}
}
当单击测试的“全部运行”按钮时,使用我的Entity Framework上下文的测试将失败,并显示此错误。
System.InvalidOperationException: The default DbConfiguration instance was used by the Entity Framework before the 'MyEntityConfiguration' type was discovered. An instance of 'MyEntityConfiguration' must be set at application start before using any Entity Framework features or must be registered in the application's config file.
但是,如果我突出显示所有测试并单击“运行选定的测试”,则所有测试都会成功。
如果我编辑测试项目的app.config
并在codeConfigurationType="MySolution.Data.MyEntityConfiguration, MySolution.Data"
行上添加<entityFramework>
,则单击Run All,可以使测试成功。
我的问题是:
codeConfigurationType
放入我的app.config
文件中才能使其工作?实体框架6.3是否不应该允许我通过DbConfiguration
来做到这一点?在我运行应用程序时,使用DbConfiguration
进行的配置可以正常运行,但在测试过程中似乎无法正常工作。