c# - 使用 EntityFramework 6 (C#) 连接到 PostgreSQL 数据库

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

我正在尝试在 C# 应用程序中使用 Entity Framework Code-First 在 PostgreSQL 服务器上创建数据库。我已经为 EF 下载了 Npgsql,并尝试以 Internet 上显示的不同方式更改我的 App.config 文件,但没有任何效果。

这是我的 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.2" />
  </startup>
  <entityFramework>
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" />
    <providers>
      <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
      <provider invariantName="Npgsql" type="Npgsql.NpgsqlServices, EntityFramework6.Npgsql" />
    </providers>
  </entityFramework>
  <connectionStrings>
    <add name="SqlServer" connectionString="Data Source=localhost;Initial Catalog=MyDB;Integrated Security=true" providerName="System.Data.SqlClient" />
    <add name="PostgreSql" connectionString="Data Source=localhost;Initial Catalog=MyDB;Integrated Security=true;User Id=postgre;Password=password;" providerName="Npgsql" />
  </connectionStrings>
</configuration>

我想补充一点,SqlServer 有效。我将不胜感激。

最佳答案

我尝试了不同的配置和选项,最终找到了解决方案。

连接字符串部分应该是这样的:

  <connectionStrings>
    <add name="SqlServer" connectionString="Data Source=localhost;Initial Catalog=SchoolDB;Integrated Security=true" providerName="System.Data.SqlClient" />
    <add name="PostgreSql" connectionString="Server=localhost;Port=5432;User Id=postgres;Password=1234;Database=SchoolDB;" providerName="Npgsql" />
  </connectionStrings>

和供应商:

<providers>
  <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
  <provider invariantName="Npgsql" type="Npgsql.NpgsqlServices, EntityFramework6.Npgsql" />
</providers>

此外,在本节末尾我添加了以下内容

  <system.data>
    <DbProviderFactories>
      <remove invariant="Npgsql" />
      <add name="Npgsql Data Provider" invariant="Npgsql" description=".Net Data Provider for PostgreSQL" type="Npgsql.NpgsqlFactory, Npgsql, Culture=neutral, PublicKeyToken=5d8b90d52f46fda7" support="FF" />
    </DbProviderFactories>
  </system.data>

它根据 DbContext 基类的调用方式在 PostgreSQL 的 SQL Server 上创建新的数据库。

关于c# - 使用 EntityFramework 6 (C#) 连接到 PostgreSQL 数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37838170/

相关文章:

c# - 从 SQL-Server 导出/导入数据子集

c# - ASP.NET EF 从未映射的类中删除鉴别器列

c# - SQL查询在转换日期后更改列名

c# - 如何从 Razor View 获取产品版本

c# - 从 MongoDB 'documents' 获取所有 'collection'

android - 是否可以将 Stack、Queue 或 Hashtable 传递给 Intent?

c# - EF : manual (non autogenerated) keys require ad hoc handling of new entities

c# - windiff 样式垂直线

c# - MySql Entity Framework 自动增量错误

c# - COM 异常 80010108 是什么意思?