为EntityFramework6.Npgsql指定数据库连接字符串 - 代码优先

时间:2017-07-22 22:18:57

标签: c# .net postgresql entity-framework-6 npgsql

我有关于.NET postgreSQL提供程序包EntityFramework6.Npgsql的问题。我安装了以下软件包:EntityFramework 6.0.0, EntityFramework6.NpgSql 3.1.1, Npgsql 3.1.0

目前我刚刚开始了一个WPF项目,所以我使用了一个简单的标准DbContext,它看起来像这样:

public class TimetrackerDbContext : DbContext
{
    public TimetrackerDbContext()
        : base("name=DbConnection")
    {
    }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.HasDefaultSchema("public");
        base.OnModelCreating(modelBuilder);
    }

    public virtual DbSet<MyEntity> MyEntities { get; set; }
}

public class MyEntity
{
    public int Id { get; set; }
    public string Name { get; set; }
}

现在在我的App.config文件中,我指定了连接字符串。我一直试图使用这种联系:

<add name="DbConnection"
   connectionString="User ID=xxx;Password=xxx;Host=xxx;Port=5432;Database=xxx;Pooling=true;"
   providerName="System.Data.EntityClient" /> 

但是当我在PM控制台中运行enable-migrations -contexttypename MyDbContext时,我收到以下错误:

Keyword not supported: 'user id'.

我尝试了对元数据,提供程序连接字符串进行specyfying,但它也引发了错误,在Code First中使用它没有多大意义。如果有人在这里使用我的整个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>
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.1" />
  </startup>
  <entityFramework>
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
      <parameters>
        <parameter value="v11.0" />
      </parameters>
    </defaultConnectionFactory>
    <providers>
      <!--<provider invariantName="System.Data.SqlClient"     type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />-->
      <provider invariantName="Npgsql" type="Npgsql.NpgsqlServices, EntityFramework6.Npgsql" />
    </providers>
  </entityFramework>
  <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="Npgsql" publicKeyToken="5d8b90d52f46fda7" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-3.1.0.0" newVersion="3.1.0.0"         />
      </dependentAssembly>
    </assemblyBinding>
  </runtime>
  <connectionStrings>
     <add name="DbConnection"
   connectionString="User ID=xxx;Password=xxx;Host=xxx;Port=5432;Database=xxx;Pooling=true;"
   providerName="System.Data.EntityClient" />
  </connectionStrings>
</configuration>

1 个答案:

答案 0 :(得分:2)

连接字符串providerName的{​​{1}}用于数据库优先(edmx)类型连接,其中实际提供程序以System.Data.EntityClient值进行编码。

对于Code First连接,connectionString应与providerName部分中的invariantName匹配。在您的情况下,替换

providers

providerName="System.Data.EntityClient"