• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 4249
  • Last Modified:

Postgresql (Npgsql) and DbProviderFactory

I am building a .net website. and want to use the DBProviderFactory with Npsql, how this can be done?

I tried doing :
DbProviderFactory factory = DbProviderFactories.GetFactory('Npgsql');

but it's not finding the provider. Is there something I have to do to make this work?
0
Dirar Abu Kteish
Asked:
Dirar Abu Kteish
  • 7
  • 4
  • 2
1 Solution
 
angus_young_acdcCommented:
Hi dxz2,

Have you definately made sure that you have this using statement at the top of your code

using System.Data.Common;
0
 
angus_young_acdcCommented:
Also not sure if its different between C# in apps and websites but I was getting an error until I used "Npgsql" instead of 'Npgsql'   It said "Too man characters in character literal."
0
 
Bob LearnedCommented:
0
Get your Disaster Recovery as a Service basics

Disaster Recovery as a Service is one go-to solution that revolutionizes DR planning. Implementing DRaaS could be an efficient process, easily accessible to non-DR experts. Learn about monitoring, testing, executing failovers and failbacks to ensure a "healthy" DR environment.

 
Dirar Abu KteishAuthor Commented:
Thanks for the replies,

Yes, I am sure about the included files.
TheLearnedOn,  I've seen it but didn't understand anything about it. I will go thought it again

0
 
Dirar Abu KteishAuthor Commented:
Hello TheLearnedOn,

where shall I put this code, and should I add something to the machine.config?
0
 
Dirar Abu KteishAuthor Commented:
I tried this: http://fxjr.blogspot.com/2006_11_01_archive.html, but not I get:
Could not load file or assembly 'Npgsql, Version=1.0.0.0, Culture=neutral, PublicKeyToken=5d8b90d52f46fda7' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)

any suggestion?

0
 
Bob LearnedCommented:
Try this under <DbProviderFactories> in machine.config:

<add name="PostgreSQL Data Provider" invariant="Npgsql"
description=".Net Framework Data Provider for PostgreSQL" type="Npgsql.NpgsqlFactory, System.Data, Version=2.0.0.0,
Culture=neutral, PublicKeyToken=b77a5c561934e089" />

The key used to get the factory is the 'invariant' value for the entry.  This is the entry for the SQL Server client:

      <add name="SqlClient Data Provider" invariant="System.Data.SqlClient" description=".Net Framework Data Provider for SqlServer" type="System.Data.SqlClient.SqlClientFactory, System.Data, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />

For that entry, you would use GetFactory("System.Data.SqlClient").

Bob
0
 
Dirar Abu KteishAuthor Commented:
hello Bob,
I am still new to .net, so please don't mind my questions :)
The machine.config is under the framework directory, and to configure it I would need administration account? is there another to do it in my website application?

-dirar
0
 
Bob LearnedCommented:
1) If the folder that you need to access for the machine.config is on the remote server, then you need administrative rights.

2) Configuration of a web site is a combination of the machine.config and a web.config file.

3) Here is an article that talks about MySql:

http://www.15seconds.com/Issue/060317.htm

<Quote>
web.config

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <system.data>
        <DbProviderFactories>
            <add
                name="MySQLDirect .NET Data Provider"
                invariant="CoreLab.MySql"
                description="CoreLab MySQLDirect .NET Data Provider"
                type="CoreLab.MySql.MySqlDirectFactory, CoreLab.MySql, Version=3.50.10.0, Culture=neutral, PublicKeyToken=09af7300eec23701"
            />
        </DbProviderFactories>
    </system.data>
</configuration>

</Quote>

Bob
0
 
Dirar Abu KteishAuthor Commented:
This is what I added on the web.config before:
<system.data>
        <DbProviderFactories>
<add name="PostgreSQL Data Provider" invariant="Npgsql"
description=".Net Framework Data Provider for PostgreSQL" type="Npgsql.NpgsqlFactory, System.Data, Version=2.0.0.0,
Culture=neutral, PublicKeyToken=b77a5c561934e089"
</DbProviderFactories>
    </system.data>

and it gave me this error:
Could not load file or assembly 'Npgsql, Version=1.0.0.0, Culture=neutral, PublicKeyToken=5d8b90d52f46fda7' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)

I got the code from: http://fxjr.blogspot.com/2006_11_01_archive.html

it's happening when I write:

DbProviderFactories.GatFactory('Npgsql');

I tried changing the version to Version=2.0.0.0 and I got the same error

-dirar
0
 
Bob LearnedCommented:
Does the \bin folder have the older Npgsql DLL?

There is a way to debug assembly load failures:

Debugging Assembly Loading Failures
http://blogs.msdn.com/suzcook/archive/2003/05/29/57120.aspx

Bob
0
 
Dirar Abu KteishAuthor Commented:
OK. I will try it. I'll be back tomorrow

thanks

-dirar
0
 
Dirar Abu KteishAuthor Commented:
Man I love you, thanks a lot...

after I ran fuslogvw.exe, I got this:
-----------------------------------------------------------------------------------------------------------------------------------------------------------------

*** Assembly Binder Log Entry  (7/29/2007 @ 9:07:30 AM) ***

The operation failed.
Bind result: hr = 0x80131040. No description available.

Assembly manager loaded from:  C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\mscorwks.dll
Running under executable  C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\WebDev.WebServer.EXE
--- A detailed error log follows.

=== Pre-bind state information ===
LOG: User = MyUser\MyUser
LOG: DisplayName = Npgsql, Version=2.0.0.0, Culture=neutral, PublicKeyToken=5d8b90d52f46fda7
 (Fully-specified)
LOG: Appbase = file:///D:/work/tests/ASP.Net/WebSite3/
LOG: Initial PrivatePath = D:\work\tests\ASP.Net\WebSite3\bin
LOG: Dynamic Base = C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\Temporary ASP.NET Files\website3\e0602ba3
LOG: Cache Base = C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\Temporary ASP.NET Files\website3\e0602ba3
LOG: AppName = de12cf9d
Calling assembly : System.Data, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089.
===
LOG: This bind starts in default load context.
LOG: Using application configuration file: D:\work\tests\ASP.Net\WebSite3\web.config
LOG: Using machine configuration file from C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\config\machine.config.
LOG: Post-policy reference: Npgsql, Version=2.0.0.0, Culture=neutral, PublicKeyToken=5d8b90d52f46fda7
LOG: GAC Lookup was unsuccessful.
LOG: Attempting download of new URL file:///C:/WINDOWS/Microsoft.NET/Framework/v2.0.50727/Temporary ASP.NET Files/website3/e0602ba3/de12cf9d/Npgsql.DLL.
LOG: Attempting download of new URL file:///C:/WINDOWS/Microsoft.NET/Framework/v2.0.50727/Temporary ASP.NET Files/website3/e0602ba3/de12cf9d/Npgsql/Npgsql.DLL.
LOG: Attempting download of new URL file:///D:/work/tests/ASP.Net/WebSite3/bin/Npgsql.DLL.
LOG: Assembly download was successful. Attempting setup of file: D:\work\tests\ASP.Net\WebSite3\bin\Npgsql.dll
LOG: Entering download cache setup phase.
LOG: Assembly Name is: Npgsql, Version=1.97.1.0, Culture=neutral, PublicKeyToken=5d8b90d52f46fda7
WRN: Comparing the assembly name resulted in the mismatch: Major Version
ERR: The assembly reference did not match the assembly definition found.
ERR: Setup failed with hr = 0x80131040.
ERR: Failed to complete setup of assembly (hr = 0x80131040). Probing terminated.

-----------------------------------------------------------------------------------------------------------------------------------------------------------------
This line was important:
-> LOG: Assembly Name is: Npgsql, Version=1.97.1.0, Culture=neutral, PublicKeyToken=5d8b90d52f46fda7

and according to the version and the PublicKeyToken as shown above I changed the configuration (web.config) to:
<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=1.97.1.0, Culture=neutral, PublicKeyToken=5d8b90d52f46fda7" />
    </DbProviderFactories>
  </system.data>  

Then I wrote:

DbProviderFactory dbPro = DbProviderFactories.GetFactory("Npgsql");

and it worked. No more errors for this one :)

Thanks man

-dirar
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 7
  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now