代码优先数据库和 PostgreSql 的问题

Problem with Code-First Database and PostgreSql

我正在使用 Code-First 项目,我的数据库是 PostgreSql。当我使用 Npgsql v.2.2.7 将我的项目迁移到 PostgreSql 时,一切正常。我需要使用新版本的 Npsql。为了实现我的目标,我将 Npsql 升级到最新版本 (4.1.5)。现在,当我使用添加迁移时,出现以下错误:

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.

我已经阅读了很多关于这个问题的文章,但其中 none 帮助我解决了我的问题。另外,我在我的项目中使用了 Dapper Contrib。我上面说的升级Npgsql后出现的问题

这个包的正确使用方法是什么?

这是我的 App.Config 文件

<?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>

安装的软件包是:

经过一段时间的搜索,我们在所有解决方案的项目中安装了 EntityFramework6.Npgsql 6.4.1 包(有一些 class 库和 WinApps 项目在解决方案)。随着这个包的更新,Npgsql 也更新到新版本 (4.1.3)。所以 app.config 更改为:

<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>

它需要更改 app.config 如下。就在DataContext所在的项目中(DataLayer)

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

替换为:

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

最后,我们必须将 DataLayer (With DataContext) 设置为 StartUp Project。 (设置为启动项目)然后我们可以使用 add-migration 迁移代码优先项目与 PostgreSql 数据库update-database PackageManagerConsole 中的命令。