首页 > 解决方案 > Code-First 数据库和 PostgreSql 的问题

问题描述

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

无法加载在应用程序配置文件中为具有不变名称“Npgsql”的 ADO.NET 提供程序注册的实体框架提供程序类型“Npgsql.NpgsqlServices,Npgsql.EntityFramework”。确保使用程序集限定名称并且程序集可用于正在运行的应用程序。有关详细信息,请参阅 http://go.microsoft.com/fwlink/?LinkId=260882

我已经阅读了很多关于这个问题的文章,但没有一篇文章能帮助我解决我的问题。另外,我在我的项目中使用了 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>

已安装的软件包是:

标签: c#postgresqldappercode-firstnpgsql

解决方案


经过一段时间的搜索,我们在所有解决方案的项目中安装了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 (With DataContext) 设置为 StartUp Project。(设置为启动项目),然后我们可以通过在PackageManagerConsole中使用add-migrationupdate-database命令来使用PostgreSql数据库迁移代码优先项目。


推荐阅读