Earlz
Earlz

Reputation: 63895

DbProviderFactory with Npgsql?

I have a project which I'm trying to port from SQL Server to PostgreSQL. I've got almost everything done I believe except for I can not get DbProviderFactory to work with Npgsql.

Factory = DbProviderFactories.GetFactory("Npgsql");

yields

Unhandled Exception: System.Configuration.ConfigurationErrorsException: Failed to find or load the registered .Net Framework Data Provider.

How do I fix this?

Upvotes: 22

Views: 18029

Answers (5)

Mark
Mark

Reputation: 2203

These were the steps that resolved it for me:

(1) add DbFactory provider to machine.config file located in the .NET Microsoft Frameworking folder

(2) register npgsql.dll and mono.security.dll in GAC using gacutil


Step by step details for:

(1) add DbFactory provider to machine.config

a. go to your relevant NET framework config directory (e.g. C:\Windows\Microsoft.NET\Framework64\v4.0.30319\Config)

b. edit the machine.config file and add the below line to DbProviderFactories

<DbProviderFactories>
    <add name="Npgsql Data Provider" 
         invariant="Npgsql" 
         support="FF" 
         description=".Net Framework Data Provider for Postgresql Server" 
         type="Npgsql.NpgsqlFactory, Npgsql"/>
  </DbProviderFactories>

(2) register npgsql.dll and mono.security.dll in GAC

a. check if npgsql and mono.security is in GAC folder (my GAC folder was located at C:\Windows\Microsoft.NET\assembly\GAC_MSIL)

If not, then use gacutil to install npgsql to GAC in command prompt using gacutil /i npgsql.dll

Upvotes: 1

Dude Pascalou
Dude Pascalou

Reputation: 3179

The type attribute value is important in the DbProviderFactories entry.

For me, the version number was incorrect. Correct version was :

<system.data>
  <DbProviderFactories>
    <add name="Npgsql Data Provider" 
         invariant="Npgsql" 
         support="FF" 
         description=".Net Framework Data Provider for Postgresql Server" 
         type="Npgsql.NpgsqlFactory, Npgsql, Version=2.2.3.0, Culture=neutral, PublicKeyToken=5d8b90d52f46fda7" />
  </DbProviderFactories>
</system.data>

You can retrieve the value on your project with :

typeof(Npgsql.NpgsqlFactory).AssemblyQualifiedName

Upvotes: 2

ANeves
ANeves

Reputation: 6385

Try defining a factory in your app.config:

<system.data>
  <DbProviderFactories>
    <add name="Npgsql Data Provider" invariant="Npgsql"
         description="Data Provider for PostgreSQL"
         type="Npgsql.NpgsqlFactory, Npgsql" />
  </DbProviderFactories>
</system.data>

Via http://fxjr.blogspot.pt/2013/06/npgsql-code-first-entity-framework-431.html

Upvotes: 15

user2965379
user2965379

Reputation: 1

register ngsql and mono.security dll in GAC and add dbfactory provider in machine.config for both[32 & 64 bit version ]

Upvotes: 0

Milen A. Radev
Milen A. Radev

Reputation: 62633

Have you read section 3.4 "Using Npgsql with ProviderFactory" from the fine manual?

Upvotes: 9

Related Questions