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?