【发布时间】:2016-06-15 14:22:19
【问题描述】:
我正在尝试在 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 有效。我将不胜感激。
【问题讨论】:
-
如果您的应用程序中需要这两个提供程序,您需要告诉 EF 哪一个用于您的上下文。否则,删除 SqlServer 条目并将默认连接工厂更改为 Npgsql 提供的连接工厂。
-
在上下文类构造函数中,我使用的是 base("PostgreSql")。默认应该是什么样子的?
-
应该够了。使用上下文时是否遇到任何错误?
-
@Spivonious 是的。 “具有不变名称 'Npgsql' 的 ADO.NET 提供程序未在机器或应用程序配置文件中注册,或者无法加载。有关详细信息,请参阅内部异常。”
-
我已经更改了“Server=localhost;Port=5432;User Id=postgres;Password=password;”的连接字符串我认为它连接起来。它在 ctx.SaveChanges();带有 npgsql 异常 42601
标签: c# .net entity-framework postgresql entity-framework-6