I'm writing a reusable base repository class where the developer will pass in a generic representing the ObjectContext
and the base repository will create an instance of it with Activator.CreateInstance
. When debugging I want to make use of the nuget package CommunityEFProviderWrappers.EFTracingProvider
. So my code to setup the object context looks like this:
public void RenewDataContext()
{
#if DEBUG
// get the default container name
var containerName = Activator.CreateInstance<T>().DefaultContainerName;
// create an instance of the object context using EF Trace
Context = (T)Activator.CreateInstance(typeof(T), EFTracingProviderUtils.CreateTracedEntityConnection(containerName));
Context.EnableTracing();
#else
Context = Activator.CreateInstance<T>();
#endif
}
The problem is that this always throws the following error when it tries to create an instance of the ObjectContext
with the EFTracingProvider
: "Schema specified is not valid. Errors: \r\n(0,0) : error 0175: The specified store provider cannot be found in the configuration, or is not valid."
If I replace containerName with the name of the connection string in the web config and don't do the first Activator.CreateInstance<T>()
then it works fine. So the issue has something to do with the fact that I create the first instance and then the second.
Here is what I have tried:
- dispose and null out the first instance.
- close the connection on the first instance.
- put the first instance in a using statement.
- explicitly define the assembly containing the ObjectContext in the connection string in the web.config in the startup project (MetadataException when using Entity Framework Entity Connection)
I am trying to avoid having the developer pass in the generic type of the ObjectContext
AND the name of the connection string. That seems kind of redundant.
So my question is: How do I get the connection name from the generic representing the object context and still be able to use it to create an instance of the object context using the EntityConnection generated by EF Trace?
My question is about why this method doesn't work, not about possible work arounds.