22

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?

Earlz
  • 62,085
  • 98
  • 303
  • 499

5 Answers5

15

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

ANeves
  • 6,219
  • 3
  • 39
  • 63
9

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

Milen A. Radev
  • 60,241
  • 22
  • 105
  • 110
  • 2
    Actually I read it and adding it to machine.config didn't work. I checked and the version that comes with Mono for Arch Linux(2.0.0.0) didn't support it. I upgraded to the latest version and now everything is good – Earlz Aug 02 '10 at 12:45
  • 4
    This link is broken. Refer to ANeves's answer and step 4 of this article: http://www.codeproject.com/Articles/783552/Using-PostgreSQL-with-Entity-Framework-in-ASP-NET – Scotty H Aug 26 '15 at 17:41
  • [Actual link](https://github.com/danzel/Npgsql/blob/master/docs/UserManual.html) refers to github ;) – Maciej Los May 04 '19 at 09:18
1

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
Dude Pascalou
  • 2,989
  • 4
  • 29
  • 34
1

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

Mark
  • 2,175
  • 20
  • 23
0

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

  • 4
    This answer could be more useful with some code examples to illustrate how to register assemblies in the GAC and how to modify machine.config, along with how to locate them. – Colin Young Nov 07 '13 at 15:29