Search code examples
c#postgresqldappercode-firstnpgsql

Problem with Code-First Database and PostgreSql


I am using Code-First project and my database is PostgreSql. When I migrated my project to PostgreSql by using Npgsql v.2.2.7 everything worked fine. I need to use the new version of Npsql. To achieve my goal I upgraded Npsql to the latest version (4.1.5). Now when I use add-migration I get the following error :

The Entity Framework provider type 'Npgsql.NpgsqlServices, Npgsql.EntityFramework' registered in the application config file for the ADO.NET provider with invariant name 'Npgsql' could not be loaded. Make sure that the assembly-qualified name is used and that the assembly is available to the running application. See http://go.microsoft.com/fwlink/?LinkId=260882 for more information.

I have read many articles about the issue but none of them assisted me in solving my problem. Also, I used Dapper Contrib in my project. The problem occurred after upgrading Npgsql as I mentioned above.

What is the correct way to use this package?

here is my App.Config File

<?xml version="1.0" encoding="utf-8"?>
    <configuration>
      <configSections>
      <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
      <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
      </configSections>
      <entityFramework>
        <providers>
          <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
          <provider invariantName="Npgsql" type="Npgsql.NpgsqlServices, Npgsql.EntityFramework" />
        </providers>
      </entityFramework>
      <system.data>
        <DbProviderFactories>
          <add name="Npgsql Data Provider" invariant="Npgsql" support="FF" description="Data Provider for PostgreSQL" type="Npgsql.NpgsqlFactory, Npgsql" />
        </DbProviderFactories>
      </system.data>
      <connectionStrings>
        <add name="KMSystemContext" connectionString="Server=****;Database=KMSystem;User Id=postgres;Password=***;" providerName="Npgsql" />
      </connectionStrings>
      <runtime>
        <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
          <dependentAssembly>
            <assemblyIdentity name="System.Runtime.CompilerServices.Unsafe" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
            <bindingRedirect oldVersion="0.0.0.0-4.0.6.0" newVersion="4.0.6.0" />
          </dependentAssembly>
          <dependentAssembly>
            <assemblyIdentity name="System.Memory" publicKeyToken="cc7b13ffcd2ddd51" culture="neutral" />
            <bindingRedirect oldVersion="0.0.0.0-4.0.1.1" newVersion="4.0.1.1" />
          </dependentAssembly>
          <dependentAssembly>
            <assemblyIdentity name="System.ComponentModel.Annotations" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
            <bindingRedirect oldVersion="0.0.0.0-4.2.1.0" newVersion="4.2.1.0" />
          </dependentAssembly>
          <dependentAssembly>
            <assemblyIdentity name="System.Threading.Tasks.Extensions" publicKeyToken="cc7b13ffcd2ddd51" culture="neutral" />
            <bindingRedirect oldVersion="0.0.0.0-4.2.0.1" newVersion="4.2.0.1" />
          </dependentAssembly>
          <dependentAssembly>
            <assemblyIdentity name="System.Numerics.Vectors" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
            <bindingRedirect oldVersion="0.0.0.0-4.1.4.0" newVersion="4.1.4.0" />
          </dependentAssembly>
          <dependentAssembly>
            <assemblyIdentity name="System.Buffers" publicKeyToken="cc7b13ffcd2ddd51" culture="neutral" />
            <bindingRedirect oldVersion="0.0.0.0-4.0.3.0" newVersion="4.0.3.0" />
          </dependentAssembly>
          <dependentAssembly>
            <assemblyIdentity name="System.ValueTuple" publicKeyToken="cc7b13ffcd2ddd51" culture="neutral" />
            <bindingRedirect oldVersion="0.0.0.0-4.0.3.0" newVersion="4.0.3.0" />
          </dependentAssembly>
        </assemblyBinding>
      </runtime>
    </configuration>

Installed packages are :

  • Npgsql 4.1.5
  • EntityFramework 6.0.0.0

Solution

  • After a while of searching we installed EntityFramework6.Npgsql 6.4.1 package in all solution's projects (There are some class liberaries and WinApps projects in the solution). With the update of this package, Npgsql update to the new version (4.1.3) too. So the app.config change to :

    <entityFramework>
        <providers>
          <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
          <provider invariantName="Npgsql" type="Npgsql.NpgsqlServices, Npgsql.EntityFramework" />
        </providers>
        <defaultConnectionFactory type="Npgsql.NpgsqlConnectionFactory, EntityFramework6.Npgsql" />
      </entityFramework>
    

    It needs to be changed app.config as below. Just in the project that DataContext has located (DataLayer)

    <provider invariantName="Npgsql" type="Npgsql.NpgsqlServices, Npgsql.EntityFramework" />
    

    Replace with :

    <provider invariantName="Npgsql" type="Npgsql.NpgsqlServices, EntityFramework6.Npgsql" />
    

    at the end, we have to set DataLayer (With DataContext) as StartUp Project. (Set as StartUp Project) and then we can migrate code first project with PostgreSql database by using add-migration and update-database commands in PackageManagerConsole.