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

问题描述

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

在应用程序配置文件注册的Entity Framework提供程序类型'Npgsql.NpgsqlServices,Npgsql.EntityFramework' 无法加载具有不变名称“ Npgsql”的ADO.NET提供程序。 确保使用了程序集限定名称,并且 程序集可用于正在运行的应用程序。看到 http://go.microsoft.com/fwlink/?LinkId=260882了解更多信息。

我阅读了许多有关此问题的文章,但没有一篇文章能帮助我解决问题。另外,我在项目中使用了Dapper Contrib。如上所述,升级Npgsql后出现了问题。

使用此软件包的正确方法是什么?

这是我的App.Config文件

<?xml version="1.0" encoding="utf-8"?>
    <configuration>
      <configSections>
      <!-- For more @R_122_4045@ion 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>

已安装的软件包为:

  • Npgsql 4.1.5
  • EntityFramework 6.0.0.0

解决方法

一段时间搜索后,我们在所有解决方案的项目中安装了 EntityFramework6.Npgsql 6.4.1 软件包(解决方案中有一些类库和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(带有DataContext)设置为Startup Project。 (设置为StartUp Project),然后我们可以使用 add-migration update-database 命令在 PostgreSql 数据库中迁移代码第一个项目。 em> PackageManagerConsole 。