我想在C#应用程序中使用实体框架代码优先在PostgreSQL服务器上创建数据库。我已经为EF下载了Npgsql,并试图以互联网上提供的不同方式更改我的App.config文件,但没有任何作用。使用EntityFramework 6(C#)连接到PostgreSQL数据库
这是我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提供的连接工厂。 – Spivonious
在Context类的构造函数中,我使用了base(“PostgreSql”)。应该如何默认样子? – Niko
这应该够了。使用上下文时是否出现错误? – Spivonious