Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

EF4 Creating a custom DbFactoryProvider and other Db* classes

I was attempting to create a set of custom classes to create my own provider that the Entity Framework would be communicating with. I had created my custom class that inherited from the abstract class DbProviderFactory and had just begun testing to see if I could get it to talk with EF, but I ran into issues.

<configuration>
  <system.data>
    <DbProviderFactories>
      <add name="Custom Data Provider"
           invariant="CustomClient"
           description=".Net Framework Data Provider for Custom"
           type="CustomClient.CustomProviderFactory, CustomClient, 
           Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" />
    </DbProviderFactories>
  </system.data>
  <connectionStrings>
    <add name="AdventureWorksEntities" 
     connectionString="metadata=...;
     provider=CustomClient;
     provider connection string=&quot;data source=.;
     initial catalog=AdventureWorks;integrated security=True;
     multipleactiveresultsets=True;App=EntityFramework&quot;"
     providerName="System.Data.EntityClient" />
  </connectionStrings>
</configuration>

With my CustomClient defined above I thought it would be smooth sailing into Entity goodness, but when my code attempts to create a new AdventureWorksEntities object I get the following exception:

InvalidOperationException The requested .Net Framework Data Provider's implementation does not have an Instance field of a System.Data.Common.DbProviderFactory derived type.

Looking over the available overrides for DbProviderFactory makes no mention of an Instance member. I'm not really sure why it is requiring this field if it is not part of the class/interface for DbProviderFactory. Is the fix as simple as providing a field named Instance that returns an object of my custom type?

like image 562
ShelbyZ Avatar asked Dec 03 '25 16:12

ShelbyZ


1 Answers

Add this to your custom provider:

public static readonly CustomProviderFactory Instance = new CustomProviderFactory();

All build in factories have this - they are used as singleton.

like image 165
Ladislav Mrnka Avatar answered Dec 05 '25 09:12

Ladislav Mrnka



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!