Assemblies
Entity Framerok install from Nuget repository, Npgsql clone git repository (https://github.com/franciscojunior/Npgsql2.git) and compile as fot .NET 4.5, reference it in your project with assemby Npgsql.EntityFramework.Configuration
Add these lines to your Web.config<configSections> <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" /> </configSections> <entityFramework> <providers> <provider invariantName="Npgsql" type="Npgsql.NpgsqlServices, Npgsql.EntityFramework" /> </providers> </entityFramework> <system.data> <DbProviderFactories> <add name="Npgsql Data Provider" invariant="Npgsql" description="Data Provider for PostgreSQL" type="Npgsql.NpgsqlFactory, Npgsql" /> </DbProviderFactories> </system.data>
Create (and configure) your Dbcontext
public MyDbContext() : base("ConnectionStringName") { Database.SetInitializer<MyDbContext>(null); } public DbSet<User> Users { get; set; } protected override void OnModelCreating(DbModelBuilder modelBuilder) { modelBuilder.Conventions.Remove<PluralizingTableNameConvention>(); modelBuilder.HasDefaultSchema("public"); // user var user = modelBuilder.Entity<User>(); user.ToTable("user") .HasKey(t => t.Id) .Property(t => t.Id) .HasColumnName("id"); base.OnModelCreating(modelBuilder); }
Line
Database.SetInitializer<MyDbContext>(null);
is avoiding all model startup exceptions like: ERROR: 42P01: relation "dbo.__MigrationHistory" does not exist ERROR: schema "dbo" does not exist
Because Npgsql doesn't support schema creation, you must create database manually and you can use this line to skip schema migrations and creation/updates.