Reputation: 3364
I'm in a solution with 200+ projects, and one of the applications require a custom DbConfiguration, like:
public class MyDbConfiguration : DbConfiguration
{
public MyDbConfigurationDbConfiguration()
{
SetExecutionStrategy("System.Data.SqlClient",
() => new SqlAzureExecutionStrategy());
}
}
But when I introduce it, it is discovered to late during unit test (where it doesn't apply anyway). I do initialize the configuration programatically at application entry, so everything works in production:
DbConfiguration.SetConfiguration(new Office2EntityFrameworkDbConfiguration());
But the 100 or so of the projects that are unit tests don't have a point of entry (and I believe it would be bad design change all of the DbContexts (there are 70 or so) or all the unit tests (15000 or so) to make sure this unneeded DbConfiguration is initialized soon enough.
System.InvalidOperationException: The default DbConfiguration instance was used by the Entity Framework before the 'MyDbConfiguration' type was discovered. An instance of 'MyDbConfiguration' must be set at application start before using any Entity Framework features or must be registered in the application's config file. See http://go.microsoft.com/fwlink/?LinkId=260883 for more information.
ved System.Data.Entity.Infrastructure.DependencyResolution.DbConfigurationManager.EnsureLoadedForAssembly(Assembly assemblyHint, Type contextTypeHint)
ved System.Data.Entity.Infrastructure.DependencyResolution.DbConfigurationManager.EnsureLoadedForContext(Type contextType)
ved System.Data.Entity.DbContext.InitializeLazyInternalContext(IInternalConnection internalConnection, DbCompiledModel model)
ved System.Data.Entity.DbContext..ctor(DbConnection existingConnection, Boolean contextOwnsConnection)
Upvotes: 1
Views: 163
Reputation: 3364
I was puzzled about this for days. It appears the MyDbConfiguration
is only detected when it lives in the same assembly as the DbContext itself (and probably in the one in which the DbContext is instantiated too).
What I ended up doing was to move the MyDbConfiguration
to its own project where it would be the only class. And this worked!
Upvotes: 1