Entity Framework Core PostgreSQL Integration

See Entity Framework Core Integration document for the basics of the EF Core integration.

EntityFrameworkCore Project Update

  • In Acme.BookStore.EntityFrameworkCore project replace package Volo.Abp.EntityFrameworkCore.SqlServer with Volo.Abp.EntityFrameworkCore.PostgreSql
  • Update to use PostgreSQL in BookStoreEntityFrameworkCoreModule
    • Replace the AbpEntityFrameworkCoreSqlServerModule with the AbpEntityFrameworkCorePostgreSqlModule
    • Replace the options.UseSqlServer() with the options.UsePostgreSql()
  • In other projects update the PostgreSQL connection string in necessary appsettings.json files

EntityFrameworkCore.DbMigrations Project Update

  • Update to use PostgreSQL in XXXMigrationsDbContextFactory
    • Replace the new DbContextOptionsBuilder<XXXMigrationsDbContext>().UseSqlServer() with the new DbContextOptionsBuilder<XXXMigrationsDbContext>().UseNpgsql()

Delete Existing Migrations

Delete all existing migration files (including DbContextModelSnapshot)

postgresql-delete-initial-migrations

Regenerate Initial Migration

Set the correct startup project (usually a web project)

set-as-startup-project

Open the Package Manager Console (Tools -> Nuget Package Manager -> Package Manager Console), select the .EntityFrameworkCore.DbMigrations as the Default project and execute the following command:

Run Add-Migration command.

PM> Add-Migration Initial

Update the Database

You have two options to create the database.

Using the DbMigrator Application

The solution contains a console application (named Acme.BookStore.DbMigrator in this sample) that can create database, apply migrations and seed initial data. It is useful on development as well as on production environment.

.DbMigrator project has its own appsettings.json. So, if you have changed the connection string above, you should also change this one.

Right click to the .DbMigrator project and select Set as StartUp Project:

set-as-startup-project

Hit F5 (or Ctrl+F5) to run the application. It will have an output like shown below:

set-as-startup-project

Using EF Core Update-Database Command

Ef Core has Update-Database command which creates database if necessary and applies pending migrations.

Set the correct startup project (usually a web project)

set-as-startup-project

Open the Package Manager Console (Tools -> Nuget Package Manager -> Package Manager Console), select the .EntityFrameworkCore.DbMigrations as the Default project and execute the following command:

PM> Update-Database

This will create a new database based on the configured connection string.

postgresql-update-database

Using the .DbMigrator tool is the suggested way, because it also seeds the initial data to be able to properly run the web application.

Contributors


Last updated: November 06, 2019 Edit this page on GitHub

Was this page helpful?

Please make a selection.

To help us improve, please share your reason for the negative feedback in the field below.

Please enter a note.

Thank you for your valuable feedback!

Please note that although we cannot respond to feedback, our team will use your comments to improve the experience.

In this document
Community Talks

What’s New with .NET 9 & ABP 9?

21 Nov, 17:00
Online
Watch the Event
Mastering ABP Framework Book
Mastering ABP Framework

This book will help you gain a complete understanding of the framework and modern web application development techniques.

Learn More