0

I tried to access a PostgreSQL database using PetaPoco but i got this exception : "Could not load the PostgreSQLDatabaseProvider DbProviderFactory."
The problem is linked to the fact that the database access is done in a different assembly than my web application.
So, to avoid the exception, I currently need to load npgsql's database provider not only in my DAL assembly but in my web application too. How can I avoid that ?

Code in a DAL assembly :

try
{
    using (Database db = new PetaPoco.Database("MyDb"))
    {
        // things ...
        return true;
    }
}
catch (Exception ex)
{
    // TODO: log exception
    return false;
}

web.config of the web application :

<connectionStrings>
    <add name="MyDb" connectionString="Server=127.0.0.1;Port=5432;Database=mydatabase;User Id=the_user;Password=the_password;" providerName="Npgsql" />
</connectionStrings>

<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=3.0.5.0, Culture=neutral, PublicKeyToken=5d8b90d52f46fda7"/>
    </DbProviderFactories>
</system.data>
Spilarix
  • 1,418
  • 1
  • 13
  • 24
  • I don't think you can do that. every thing runs from in your base application, so the base application needs to have all the config params – Gelootn Apr 28 '16 at 12:18
  • @Gelootn it's ok for me to keep the configuration in my web.config (I think you're right, I'm obliged to). The real problem is the npgsql assembly which I need to load from the web application and not from my class library project containing all access to the database. – Spilarix Apr 28 '16 at 13:11
  • correct me if i am wrong, but when you reference the Data Access project your npgsql dll should also be copied to your bin dir on build. check if the npgsql dll in you dataaccess project is set to copy local in the properties – Gelootn Apr 28 '16 at 13:18
  • @Gelootn I thought too but it's not the case although "Copy local" is set to true. – Spilarix Apr 28 '16 at 14:26
  • The assembly must be accessible, else you're going to have a bad time. I would include a reference from the main project. What's the big deal with doing that anyway? – Plebsori Apr 28 '16 at 23:38

0 Answers0