Custom .NET Data Providers
Asked Answered
B

1

8

Is is possible to use a custom .NET data provider without installing it in the GAC?

Can I reference a custom DLL and register it inside my configuration file?

Bernat answered 27/2, 2012 at 15:9 Comment(0)
P
7

Yes, you can register an implementation of the DbProviderFactory class by adding the following section in your configuration file:

<system.data>
    <DbProviderFactories>
        <add name="My Custom Data Provider"
             invariant="MyCustomDataProvider" 
             description="Data Provider for My Custom Store" 
             type="MyNamespace.MyCustomProviderFactory, MyCustomDataProvider, Version=1.0.0.0, Culture=neutral, PublicKeyToken=" />
    </DbProviderFactories>
</system.data>

The MyCustomDataProvider assembly doesn't have to be registered in the GAC but can be deployed together with the application as a private assembly.

You can refer to the registered data provider programmatically by using the value specified in the invariant attribute. For example you could tell ADO.NET to use the MyNamespace.MyCustomProviderFactory by specifying MyCustomProvider as the providerName in the connection string:

<connectionStrings>
    <add name="ConnString" 
         providerName="MyCustomProvider" 
         connectionString="MyCustomConnectionString" />
</connectionStrings>

In code you can use the same provider name with the DbProviderFactories.GetFactory method:

DbProviderFactory factory = DbProviderFactories.GetFactory("MyCustomDataProvider");

where factory will be an instance of the MyNamespace.MyCustomProviderFactory class.

Pockmark answered 27/2, 2012 at 15:27 Comment(4)
That's exactly what I'm doing. I don't understand where I'm going wrong?Bernat
Have you tried to specify the fully qualified assembly name in the type attribute?Pockmark
I forgot to include the DLL in my start up project.Bernat
You saved me from a lot of trouble. Thank you very much, registering an Sybase SQL Anywhere 16 EF provider works perfectly! Code: <system.data> <DbProviderFactories> <add name="iAnywhere.Data.SQLAnywhere" invariant="iAnywhere.Data.SQLAnywhere.EF6" description="SQL Anywhere ADO.NET Provider" type="iAnywhere.Data.SQLAnywhere.SAFactory, iAnywhere.Data.SQLAnywhere.EF6, Version=16.0.0.20764, Culture=neutral, PublicKeyToken=f222fc4333e0d400" /> </DbProviderFactories> </system.data> and setting the correct assembly as dependency. Top!Rebak

© 2022 - 2024 — McMap. All rights reserved.