N
N
no0ob2016-05-14 02:41:49
PostgreSQL
no0ob, 2016-05-14 02:41:49

How to make friends Postgresql and EntityFramework?

I will abstractly describe the situation: there is a project consisting of a class library in which data access is implemented and a web project on ASP.NET MVC. I want to make sure that in the web project there are no links to the Entity Framework, but only a link to a library with classes for data access. The reference library has Entity Framework, Npgsql and Npgsql.EntityFramework. The following is written in App.config of the library:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <configSections>
    
    <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
  <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 --></configSections>
  <system.data>
    <DbProviderFactories>
      <remove invariant="Npgsql" />
      <add name="Npgsql Data Provider"
            invariant="Npgsql"
            description="Data Provider for PostgreSQL"
            type="Npgsql.NpgsqlFactory, Npgsql" />
    </DbProviderFactories>
  </system.data>
  <entityFramework>
    <defaultConnectionFactory type="Npgsql.NpgsqlFactory, Npgsql, Version=2.2.7.0, Culture=neutral, PublicKeyToken=5d8b90d52f46fda7" />
    <providers>
      <provider invariantName="Npgsql" type="Npgsql.NpgsqlServices, Npgsql.EntityFramework, Version=2.2.7.0, Culture=neutral, PublicKeyToken=5d8b90d52f46fda7" />
    </providers>
  </entityFramework>
</configuration>

In a web project, the web.config contains the following:
<configSections>
    <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
  </configSections>
  <connectionStrings>
    <add name="Auth" connectionString="server=localhost;user id=username;password=password;database=Gullfoss.Auth" providerName="Npgsql" />
  </connectionStrings>
<system.data>
    <DbProviderFactories>
      <remove invariant="Npgsql" />
      <add name="Npgsql Data Provider"
            invariant="Npgsql"
            description="Data Provider for PostgreSQL"
            type="Npgsql.NpgsqlFactory, Npgsql, Version=2.2.7.0, Culture=neutral, PublicKeyToken=5d8b90d52f46fda7" />
    </DbProviderFactories>
  </system.data>
  <entityFramework>
    <defaultConnectionFactory type="Npgsql.NpgsqlFactory, Npgsql, Version=2.2.7.0, Culture=neutral, PublicKeyToken=5d8b90d52f46fda7" />
    <providers>
      <provider invariantName="Npgsql" type="Npgsql.NpgsqlServices, Npgsql.EntityFramework, Version=2.2.7.0, Culture=neutral, PublicKeyToken=5d8b90d52f46fda7" />
    </providers>
  </entityFramework>

When starting the web application and trying to access the database, the following error occurs:
Failed to set Database.DefaultConnectionFactory to an instance of the 'Npgsql.NpgsqlFactory, Npgsql, Version=2.2.7.0, Culture=neutral, PublicKeyToken=5d8b90d52f46fda7' type as specified in the application configuration. See inner exception for details.

The InnerException has the following:
Constructor on type 'Npgsql.NpgsqlFactory' not found.

Help me figure it out, I've been scratching my head over this problem all day and I can't solve it.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
N
no0ob, 2016-05-14
@no0ob

Problem solved. Updated npgsql to 3.0.7 ( https://www.nuget.org/packages/EntityFramework6.Np... and changed configs:

<entityFramework>
    <defaultConnectionFactory type="Npgsql.NpgsqlConnectionFactory, EntityFramework6.Npgsql" />
    <providers>
      <provider invariantName="Npgsql" type="Npgsql.NpgsqlServices, EntityFramework6.Npgsql" />
    </providers>
  </entityFramework>

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

On the Internet I came across information, they say, it is correct to write like this:
I got this error because of this line.

E
eg015t, 2017-03-02
@eg015t

no0ob and is connectionStrings also in .config? That link doesn't work either...

The format of the initialization string does not match the specification

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question