I want to enable SQL logging explicit from a single integration-test, without changing the DbContext definitions
What I have: 10+ projects with multiple DbContexts. 10+ test-projects. Many other integration-tests also using EF
What I have tried:
internal class MyDbConfiguration : DbConfiguration
{
public MyDbConfiguration(StreamWriter sw)
{
var interceptor = new DatabaseLogFormatter(s => { sw.WriteLine(s); });
AddInterceptor(interceptor );
}
}
From one integration test, which is running explicit on my machine, is started by running this code
var dbConfiguration = new MyDbConfiguration(fileStream);
DbConfiguration.SetConfiguration(dbConfiguration);
This works fine. The single test is logging all EF SQLs
But when I run the full test suite (without the one test and without the SetConfiguration), EF will pick up my MyDbConfiguration (to late) and throw an Exception
System.InvalidOperationException: The default DbConfiguration instance was used by the Entity Framework before the 'MyDbConfiguration' type was discovered
How do I prevent this? I cannot change any app.configs
I want EF to use the MyDbConfiguration, only when I configure it.