迁移为什么要寻找Sql Server Provider?

时间:2015-03-06 19:53:36

标签: entity-framework postgresql entity-framework-6 npgsql

这是我的配置......

<?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时,它会在堆栈跟踪结束时给出...

  

找不到ADO.NET提供程序的实体框架提供程序   不变名称'System.Data.SqlClient'。确保提供商是   在应用程序配置的'entityFramework'部分注册   文件。有关详情,请参阅http://go.microsoft.com/fwlink/?LinkId=260882   信息。

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

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

无法将Database.DefaultConnectionFactory设置为。的实例   'Npgsql.NpgsqlFactory,Npgsql'在应用程序中指定的类型   配置。

我错过了什么?

2 个答案:

答案 0 :(得分:0)

您可以尝试使用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> 

答案 1 :(得分:0)

我通过添加Entityframework,Npgsql和Npgsql.Entityframework作为使用Nuget的三个单独安装来实现这一点。然后是以下配置......

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