Problem with connection between EF and PostgreSQL

3

I'm trying to make a connection to EF and PostgreSQL in an Asp.Net Mvc application

I'm 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 is this way this way link

 <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 execute 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 type initiator of 'Npgsql.Counters' has thrown an exception.

What would be the problem with my WebConfig?

    
asked by anonymous 18.02.2017 / 18:48

2 answers

1

It looks like some version mismatch of assemblies. Try updating all references via nuget. I use slightly 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>
    
19.02.2017 / 19:58
1

I have also been able to solve this problem through the following link link

    
08.03.2017 / 14:53