Reputation: 5988
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:
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.
Upvotes: 17
Views: 2113
Reputation: 1
my friend is so simple that it goes unnoticed, its configuration file is not with the connection string in the same Project view (StartUP Project), if not in the asp.net web.config and if windows form is not in app.config Project (View) windows Form (UI).
Upvotes: 0
Reputation: 2415
Here is your solution, add the folowing code to your config file:
<system.data>
<DbProviderFactories>
<add name="EF Caching Data Provider"
invariant="EFCachingProvider"
description="Caching Provider Wrapper"
type="EFCachingProvider.EFCachingProviderFactory, EFCachingProvider, Version=1.0.0.0, Culture=neutral, PublicKeyToken=def642f226e0e59b" />
<add name="EF Tracing Data Provider"
invariant="EFTracingProvider"
description="Tracing Provider Wrapper"
type="EFTracingProvider.EFTracingProviderFactory, EFTracingProvider, Version=1.0.0.0, Culture=neutral, PublicKeyToken=def642f226e0e59b" />
<add name="EF Generic Provider Wrapper"
invariant="EFProviderWrapper"
description="Generic Provider Wrapper"
type="EFProviderWrapperToolkit.EFProviderWrapperFactory, EFProviderWrapperToolkit, Version=1.0.0.0, Culture=neutral, PublicKeyToken=def642f226e0e59b" />
</DbProviderFactories>
</system.data>
The problem occurs in EFTracingProviderUtils.CreateTracedEntityConnection
method. It searches for .Net Framework Data Provider - EFTracingProvider, which must be specified in your config file.
When it can't find it, it can't build such connection string for its purposes:
"metadata=reader://998e0526-8372-4bf9-83d3-949dececce96; provider=EFTracingProvider; provider connection string=\"wrappedProvider=;data source=.\SQLEXPRESS;attachdbfilename=|DataDirectory|\database.mdf; integrated security=True; connect timeout=30; user instance=True; multipleactiveresultsets=True; App=EntityFramework\";"
As you can see metadata reader specified it this connection string, here will be MetadataException which is thrown when EF can't find EFTracingProvider. It is just problem with third-party method.
You can call next method and receive same result without any changes to your config:
EntityConnectionWrapperUtils.CreateEntityConnectionWithWrappers(containerName, new string[]{});
Upvotes: 1
Reputation: 26698
Not really direct answer.
I would not recommend hardcode assumption that such configuration exists:
(T)Activator.CreateInstance(typeof(T), EFTracingProviderUtils.CreateTracedEntityConnection(containerName));
There are quite rare cases (outside of simple examples) when people store EF connection string to the config files under default name. I think it might be better to accept connection string or apply some sort of convention.
FYI. DbContext
does not expose DefaultContainerName
directly. You need to get ObjectContext
for it first.
Upvotes: 0