Reputation: 2086
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.
Upvotes: 3
Views: 373
Reputation: 23234
If you don't want Entity Framework
to detect and pick up your custom DbConfiguration
automatically, you'll have find to a way to bypass the auto detection rules.
By default (without any additional custom configuration) the DbConfigurationFinder
of Entity Framework
(source code) looks for a type derived from DbConfiguration
within the same assembly as where the DbContext
derived class is declared.
Because you can't change any .config
files, one way to do this is by declaring your DbConfiguration
derived class in a separate assembly, away from the one holding the DbContext
.
Since you're using it for integration testing purposes, you can put it into that test project.
Upvotes: 3