Error when connecting PostgreSQL to the Entity Framework

4

I'm trying to connect to EF in Postgresql. You are running the following message:

  An unhandled exception of type   'System.Configuration.ConfigurationErrorsException' occurred in   System.Configuration.dll

     

Additional information: An error occurred while creating the   system.data configuration section: The 'InvariantName' column is   restricted to contain unique values. The value 'Npgsql' already exists.

My Context Class:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.Entity;
using Npgsql;

namespace TesteEnti
{
    class testeBD: DbContext
    {

        public testeBD()
            : base("TesteDB")
        {
        }

        public DbSet<Usuario> Usuario { get; set; }
    }
}

My 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" />
  </startup>
  <system.data>
    <DbProviderFactories>
      <add name="Npgsql Data Provider"
            invariant="Npgsql"
            description="Data Provider for PostgreSQL"
            type="Npgsql.NpgsqlFactory, Npgsql" />
    </DbProviderFactories>
  </system.data>
  <connectionStrings>
    <add name ="TesteDB" connectionString="server=localhost;user id=postgres;password=flavio123;database=testedb" providerName="Npgsql"/>
  </connectionStrings>
  <entityFramework>
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" />
    <providers>
      <provider invariantName="Npgsql" type="Npgsql.NpgsqlServices, Npgsql.EntityFramework" />
    </providers>
  </entityFramework>
</configuration>

I have packages installed, EF and NPGSQL EF.

What could I do to solve this problem?

    
asked by anonymous 24.03.2017 / 02:11

1 answer

3

You need to add the remove tag before the add tag, in case the provider already has it removed and then added again.

 <DbProviderFactories>
      <remove invariant="Npgsql"/>
      <add name="Npgsql Data Provider"
            invariant="Npgsql"
            description="Data Provider for PostgreSQL"
            type="Npgsql.NpgsqlFactory, Npgsql" />
    </DbProviderFactories>
    
24.03.2017 / 04:01