为什么 Migrations 正在寻找 Sql 服务器提供商?

Why is Migrations looking for Sql Server Provider?

这是我的配置...

<?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" />
  </configSections>
  <connectionStrings>
    <add name="DataContext" connectionString="Server=localhost;Port=5432;Database=myDatabase;User Id=postgres;Password=password;Preload Reader = true;" providerName="Npgsql" />
  </connectionStrings>
  <entityFramework>
    <providers>
      <provider invariantName="Npgsql" type="Npgsql.NpgsqlServices, Npgsql.EntityFramework" />
    </providers>
  </entityFramework>
</configuration>

当我 运行 update-database 它在堆栈跟踪的末尾给出...

No Entity Framework provider found for the ADO.NET provider with invariant name 'System.Data.SqlClient'. Make sure the provider is registered in the 'entityFramework' section of the application config file. See http://go.microsoft.com/fwlink/?LinkId=260882 for more information.

当我添加到 entityFramework 部分时 运行 我遇到了另一个错误..

 <defaultConnectionFactory type="Npgsql.NpgsqlFactory, Npgsql" />

Failed to set Database.DefaultConnectionFactory to an instance of the 'Npgsql.NpgsqlFactory, Npgsql' type as specified in the application configuration.

我错过了什么?

您可以尝试使用 DbProvidersFactories 而不是连接字符串:

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

我通过使用 Nuget 添加 Entityframework、Npgsql 和 Npgsql.Entityframework 作为三个单独的安装来实现此功能。然后下面的配置...

<?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" />
  </configSections>
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
  </startup>
  <connectionStrings>
    <add name="[Name of EF Context class]" connectionString="Server=localhost;Port=5432;Database=[Name of existing database];User Id=[username];Password=[password];" providerName="Npgsql" />
  </connectionStrings>
  <entityFramework>
    <providers>
      <provider invariantName="Npgsql" type="Npgsql.NpgsqlServices, Npgsql.EntityFramework"></provider>
    </providers>
    <defaultConnectionFactory type="Npgsql.NpgsqlConnectionFactory, Npgsql.EntityFramework" />
  </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>
</configuration>