Error Connecting Postgresql to Entity Framework

Asked

Viewed 459 times

5

I am trying to connect the EF in Postgresql. You are presenting 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 exclusive values. Npgsql value already exists.

My Class Context:

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 the packages installed, EF and NPGSQL EF.

What I could do to solve this problem?

  • Take the entire dbProvidersFactory tag from the web.config and test

1 answer

4


You need to add the tag remove before the tag add, in case there already exists the Preview it remove and then add again.

 <DbProviderFactories>
      <remove invariant="Npgsql"/>
      <add name="Npgsql Data Provider"
            invariant="Npgsql"
            description="Data Provider for PostgreSQL"
            type="Npgsql.NpgsqlFactory, Npgsql" />
    </DbProviderFactories>
  • It worked thanks, but how can I change so that it does not save as a DBO and yes in the schema that I wish ? For he takes from SQLSERVER’s standard the DBO.

  • 1

    You can use this [System.ComponentModel.Dataannotations.Schema.Table("[name of your table]", Schema = "public")]

  • Yes I did so , only I still create a Shema DBO with a Migration table. How can I remove this so that it is all in the public schema?

  • IN your Dbcontext Inserts this line modelBuilder.HasDefaultSchema("public"); in the method OnModelCreating Thus remaining: protected override void OnModelCreating(DbModelBuilder modelBuilder)&#xA; {&#xA; modelBuilder.HasDefaultSchema("public");&#xA; base.OnModelCreating(modelBuilder);&#xA; }

Browser other questions tagged

You are not signed in. Login or sign up in order to post.