Skip to content

Latest commit

 

History

History
56 lines (41 loc) · 2.53 KB

File metadata and controls

56 lines (41 loc) · 2.53 KB

Getting Started

Npgsql has an Entity Framework Core provider. It mostly behaves like a any other EFCore provider (e.g. SQL Server) - all the information in the general EF Core docs applies. If you're just getting started with EF Core, those docs are the best place to start.

Development happens in the Npgsql.EntityFrameworkCore.PostgreSQL repository, all issues should be reported there.

Using the Npgsql EF Core Provider

To use the Npgsql EF Core provider, simply add a dependency on Npgsql.EntityFrameworkCore.PostgreSQL. You can follow the instructions in the general EF Core Getting Started docs.

Following is an example (new-style) csproj using Npgsql EF Core:

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>netcoreapp2.0</TargetFramework>
  </PropertyGroup>
  <ItemGroup>
    <PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="2.0.0" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="2.0.0" />
  </ItemGroup>
  <ItemGroup>
    <DotNetCliToolReference Include="Microsoft.EntityFrameworkCore.Tools.DotNet" Version="2.0.0" />
  </ItemGroup>
</Project>

Using the Npgsql provider

public class BlogContext : DbContext
{
    // When used with ASP.net core, add these lines to Startup.cs
    //   var connectionString = Configuration.GetConnectionString("BlogContext");
    //   services.AddEntityFrameworkNpgsql().AddDbContext<BlogContext>(options => options.UseNpgsql(connectionString));
    // and add this to appSettings.json
    // "ConnectionStrings": { "BlogContext": "Server=localhost;Database=blog" }
    
    public BlogContext(DbContextOptions<BlogContext> options) : base(options) { }
    
    public DbSet<BlogPost> BlogPosts { get; set; }     
}

Using an Existing Database (Database-First)

The Npgsql EF Core provider also supports reverse-engineering a code model from an existing PostgreSQL database ("database-first"). To do so, execute the following if you're using dotnet cli:

dotnet ef dbcontext scaffold "Host=localhost;Database=mydatabase;Username=myuser;Password=mypassword" Npgsql.EntityFrameworkCore.PostgreSQL

Or with Powershell:

Scaffold-DbContext "Host=localhost;Database=mydatabase;Username=myuser;Password=mypassword" Npgsql.EntityFrameworkCore.PostgreSQL