Problem with connection between EF and Postgresql

Asked

Viewed 649 times

3

I am trying to make a connection with EF and Postgresql in an Asp.Net Mvc application

I am using the following references

  • Entityframework6.Npgsql
  • Npgsql 3.1
  • .NET Framework 4.5

I have the following classes:

public class Categoria
{
   [Key]
   public int Id { get; set; }
   public int Descricao { get; set; }
   public int Status { get; set; }
 }

public class MeuNegocioContext : DbContext
{
    public DbSet <Categoria> Categorias { get; set; }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        // do not use migration
        Database.SetInitializer<MeuNegocioContext>(null);
        modelBuilder.Entity<Categoria>().ToTable("Categoria", "public");
    }

}

And my Web.Config stays that way http://pastebin.com/QNmSSAyU

 <connectionStrings>
    <add name="MeuNegocioContext" connectionString="Server=127.0.0.1;Port=5432;Database=meunegocioweb;User Id=postgres;Password=6844866;" providerName="Npgsql" />
  </connectionStrings>
  <entityFramework>
    <defaultConnectionFactory type="Npgsql.NpgsqlFactory, Npgsql" />
    <providers>
      <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
      <provider invariantName="Npgsql" type="Npgsql.NpgsqlServices, EntityFramework6.Npgsql" />
    </providers>
  </entityFramework>
  <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>

However an error occurs when I try to perform the following function

 // GET: Categorias
  public ActionResult Index()
  {
     return View(db.Categorias.ToList());
  }

An Exception of type 'System.Typeinitializationexception' occurred in Npgsql.dll but was not handled in user code

Additional information: The 'Npgsql.Counters' type initializer triggered an exception.

What would be the problem related to my Webconfig?

2 answers

1

1


It seems some version incompatibility of assemblies. Try to update all references via nuget. I use a little different settings in webconfig, if help follows:

<entityFramework>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" />
<providers>
  <provider invariantName="Npgsql" type="Npgsql.NpgsqlServices, EntityFramework6.Npgsql" />
  <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
</providers>
</entityFramework>
<system.data>
<DbProviderFactories>
  <remove invariant="Npgsql" />
  <add name="Npgsql Data Provider" invariant="Npgsql" description="Data Provider for PostgreSQL" type="Npgsql.NpgsqlFactory, Npgsql" />
</DbProviderFactories>
</system.data>

Browser other questions tagged

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