EF Core Providers PostgreSQL

Help us to keep this website almost Ad Free! It takes only 10 seconds of your time:
> Step 1: Go view our video on YouTube: EF Core Bulk Insert
> Step 2: And Like the video. BONUS: You can also share it!

PostgreSQL is a general-purpose and object-relational database management system, the most advanced open-source database system.

  • PostgreSQL has been proven to be highly scalable both in the sheer quantity of data it can manage and the number of concurrent users it can accommodate.
  • It allows you to add custom functions developed using different programming languages such as C/C++, Java, etc.
  • PostgreSQL requires very minimum maintained efforts because of its stability.

Install Entity Framework Core

Let's create a new application using the Console App (.NET Core) template and install the following NuGet package.

In the Package Manager Console window, enter the following command.

PM> Install-Package Microsoft.EntityFrameworkCore

You can also install this NuGet package by right-clicking on your project in Solution Explorer and select Manage Nuget Packages....


Search for Microsoft.EntityFrameworkCore and install the latest version by pressing the install button.

Register EF Core Provider

For PostgreSQL, we need to install Npgsql.EntityFrameworkCore.PostgreSQL and will get all the packages required for EF Core.

PM> Install-Package Npgsql.EntityFrameworkCore.PostgreSQL

Now, you are ready to start your application.

Create Data Model

Model is a collection of classes to interact with the database.

  • A model stores data that is retrieved according to the commands from the Controller and displayed in the View.
  • It can also be used to manipulate the data to implement the business logic.

To create a data model for our application, we will start with the following two entities.

public class Author
    public int AuthorId { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public DateTime BirthDate { get; set; }
    public List<Book> Books { get; set; }

public class Book
    public int BookId { get; set; }
    public string Title { get; set; }
    public Author Author { get; set; }

There's a one-to-many relationship between Author and Book entities. In other words, an author can write any number of books, and only one author can write a book.

Create Database Context

The database context class provides the main functionality to coordinate Entity Framework with a given data model.

  • You create this class by deriving from the System.Data.Entity.DbContext class.
  • In your code, you specify which entities are included in the data model.
  • You can also customize certain Entity Framework behaviors.

So, let's add a new BookStore class that will inherit the DbContext class.

public class BookStore : DbContext
    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        optionsBuilder.UseNpgsql("host=localhost;user id=postgres;password=mw;database=postgres1;Pooling=false;Timeout=300;CommandTimeout=300;");
    public DbSet<Author> Authors { get; set; }
    public DbSet<Book> Books { get; set; }

In EF Core, the DbContext has a virtual method called OnConfiguring which will get called internally by EF Core.

  • It will pass in an DbContextOptionsBuilder instance, which can be used to configure options for the DbContext.
  • The DbContextOptionsBuilder has the UseNpgsql method, which expects a connection string as a parameter.

Create Database

Now, to create a database using migrations from your model, install the following packages

PM> Install-Package Microsoft.EntityFrameworkCore.Tools
PM> Install-Package Microsoft.EntityFrameworkCore.Design

Once these packages are installed, run the following command in Package Manager Console.

Add-Migration Initial

This command scaffold a migration to create the initial set of tables for your model. When it is executed successfully, then run the following command.


This command applies the new migration to the database and creates the database before applying migrations.

Now, we are done with the required classes and database creation, let's add some authors and book records to the database and then retrieve them.

using (var context = new BookStore())
    var authors = new List<Author>
        new Author
            FirstName ="Carson",
            LastName ="Alexander",
            BirthDate = DateTime.Parse("1985-09-01"),
            Books = new List<Book>()
                new Book { Title = "Introduction to Machine Learning"},
                new Book { Title = "Advanced Topics on Machine Learning"},
                new Book { Title = "Introduction to Computing"}
        new Author
            FirstName ="Meredith",
            LastName ="Alonso",
            BirthDate = DateTime.Parse("1970-09-01"),
            Books = new List<Book>()
                new Book { Title = "Introduction to Microeconomics"}
        new Author
            FirstName ="Arturo",
            LastName ="Anand",
            BirthDate = DateTime.Parse("1963-09-01"),
            Books = new List<Book>()
                new Book { Title = "Calculus I"},
                new Book { Title = "Calculus II"}


using (var context = new BookStore())
    var list = context.Authors
        .Include(a => a.Books)

    foreach (var author in list)
        Console.WriteLine(author.FirstName + " " + author.LastName);

        foreach (var book in author.Books)
            Console.WriteLine("\t" + book.Title);

If you run the application, you will see that authors and books are successfully inserted into the database and printed on the console.

Carson Alexander
        Introduction to Machine Learning
        Advanced Topics on Machine Learning
        Introduction to Computing
Meredith Alonso
        Introduction to Microeconomics
Arturo Anand
        Calculus I
        Calculus II

Got any EF Core Providers Question?