Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Connect to PostgreSQL database using EntityFramework 6 (C#)

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.

like image 876
Niko Avatar asked Jun 15 '16 14:06

Niko


People also ask

Can Entity Framework work with PostgreSQL?

Using the Entity Data Provider with Entity Framework Core. dotConnect for PostgreSQL currently supports Entity Framework Core. Entity Framework Core is supported for the Full . NET Framework platform of version 4.5.


1 Answers

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.

like image 181
Niko Avatar answered Sep 19 '22 09:09

Niko