Connect to PostgreSQL database using EntityFramework 6 (C#)

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

Question

I am trying to create database on PostgreSQL server using Entity Framework Code-First in C# application. I have downloaded Npgsql for EF and tried to change my App.config file in different ways presented on Internet, however none works.

This is my App.config file:

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

I would like to add that SqlServer works. I would be grateful for help.

1
4
6/15/2016 2:22:19 PM

Accepted Answer

I was trying a different configurations and options and finally found the solution.

Connection string section should be like:

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

And the providers:

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

Moreover, at the end of section I have added the following

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

It creates new database on SQL Server of PostgreSQL depending on how DbContext base class is called.

7
6/17/2016 9:30:01 AM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow