• Stars
    star
    102
  • Rank 334,073 (Top 7 %)
  • Language
    C#
  • Created over 7 years ago
  • Updated almost 2 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

ASP.NET 7.0 Web Application using PostgreSQL with Entity Framework

PostgreSQL ASP.NET 7

Convert an ASP.NET Core Web Application project to use PostgreSQL with Entity Framework.

This enables development of ASP.NET Core projects using VS Code on macOS or linux targets.

This project uses .NET 7.0 target framework, ASP.NET Core Web Application MVC project scaffold from Visual Studio 2022 (version 17.4).

vscode

Project setup has already been completed in this repository - assure environment setup; then, jump to running the solution.

Environment Setup

This project requires PostgreSQL - installation instructions are provided below.

If using Visual Studio Code, you will need to generate ASP.NET Core developer certificates by issuing the following commands from a terminal:

dotnet dev-certs https --clean
dotnet dev-certs https

For command line database ef commands, you will need to install Entity Framework Core tools .NET CLI:

dotnet tool install --global dotnet-ef

Project Setup

Below, instructions are referenced to use PostgreSQL in a ASP.NET Core project.

Install NuGet packages

Install the Npgsql.EntityFrameworkCore.PostgreSQL NuGet package in the ASP.NET web application.

To do this, you can use the dotnet command line by executing:

$ dotnet add package Npgsql.EntityFrameworkCore.PostgreSQL --version 3.1.2

Or, edit the project's .csproj file and add the following line in the PackageReference item group:

<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="3.1.2" />

Update appsettings.json

Configure connection string in project's appsettings.json, replacing the username, password, and dbname appropriately:

"ConnectionStrings": {
    "DefaultConnection": "User ID=username;Password=password;Server=localhost;Port=5432;Database=dbname;Integrated Security=true;Pooling=true;"
},

Modify Program.cs

Inside Program.cs replace the UseSqlServer options with UseNpgsql:

builder.Services.AddDbContext<ApplicationDbContext>(options =>
    options.UseNpgsql(connectionString));

Running the solution

Before the solution can be executed, be sure to run entity framework migrations.

Migration Issues with DbContext

Initial migrations may fail, due to ASP.NET Core template come with a pre-generation migration for SQL Server.

When trying to run the migration, you might see errors such as:

Npgsql.PostgresException (0x80004005): 42704: type "nvarchar" does not exist

System.NullReferenceException: Object reference not set to an instance of an object.

System.InvalidOperationException: No mapping to a relational type can be found for property 'Microsoft.AspNetCore.Identity.IdentityUser.TwoFactorEnabled' with the CLR type 'bool'.

Delete the entire Migrations folder, and regenerate new inital migrations.

Generate a new migration using Visual Studio Package Manager Console (from menu: Tools -> NuGet Package Manager -> Package Manager Console):

PM> Add-Migration

Or, from the command line via DotNet CLI:

$ dotnet ef migrations add Initial

If dotnet migration tools don't exist, remember to install the tools using the instruction above in the environment setup.

Run Entity Framework Migrations

Execute the migration using either Visual Studio Package Manager Console (from menu: Tools -> NuGet Package Manager -> Package Manager Console):

PM> Update-Database

Or, from the command line via DotNet CLI, execute the following command inside the project directory, where the .csproj file is located:

$ dotnet ef database update

After running the migration, the database is created and web application is ready to be run.

Setting up a PostgresSQL server on Mac

Here are instructions to setup a PostgreSQL server on Mac using Homebrew.

Installing PostgreSQL on Mac

Use brew to install PostgreSQL, then launch the service:

$ brew install postgresql
$ brew services start postgresql

Create a user

Create a user using the createuser command from a terminal, where username is your desired new user name. Using the -P argument, you will be prompted to setup a password.

$ createuser username -P

Create a database

Create your database using the createdb command from a terminal, where dbname is your desired new database name.

$ createdb dbname

At this time, run the solution's Entity Framework migrations (see above for instructions).

Verifying database

Launch PostgreSQL interactive terminal and connect to the database.

$ psql dbname

From the PostgreSQL interface terminal, List tables using the \dt command:

dbname=# \dt
                   List of relations
 Schema |         Name          | Type  |    Owner     
--------+-----------------------+-------+--------------
 public | AspNetRoleClaims      | table | username
 public | AspNetRoles           | table | username
 public | AspNetUserClaims      | table | username
 public | AspNetUserLogins      | table | username
 public | AspNetUserRoles       | table | username
 public | AspNetUserTokens      | table | username
 public | AspNetUsers           | table | username
 public | __EFMigrationsHistory | table | username
(8 rows)

Database permissions issues

If permissions were not setup properly during the creation of the database, retroactively fix by granting privileges where dbname is your database name and username is the user you created:

$ psql dbname
dbname=# GRANT ALL PRIVILEGES ON ALL TABLES IN SCHEMA public TO username;

More Repositories

1

mysql-dotnet-core

ASP.NET Core 5.0 Web Application using MySQL with Entity Framework
C#
105
star
2

lunarphase-js

Calculate phase of the moon using Julian date
TypeScript
48
star
3

threejs-sveltekit

3D with SvelteKit and Three.js
JavaScript
45
star
4

vite-typescript-npm-package

Vite TypeScript library npm package template
TypeScript
22
star
5

minimalcomps-openfl

OpenFL port of Minimal Components by Keith Peters
Haxe
22
star
6

sqlite-dotnet-core

.NET 7.0 Console Application using SQLite with Entity Framework and Dependency Injection
C#
19
star
7

typescript-npm-package

TypeScript library npm package template
JavaScript
17
star
8

perlin-toolkit

Animated perlin noise textures
ActionScript
15
star
9

minimalcomps-openfl-designer

Exploration of minimalcomps components
Haxe
12
star
10

pixi-graphpaper

Pixi.js ruled graph paper
TypeScript
11
star
11

openfl-haxe-benchmark

Benchmark segments of code across multiple platforms
Haxe
10
star
12

babylonjs-sveltekit

3D with SvelteKit and Babylon.js
JavaScript
9
star
13

swift-prototypes

Swift programming experimental prototypes
Swift
8
star
14

openfl-away3d-terrain-demo

3D terrain demo using OpenFL, Away3D, and Haxe
Haxe
7
star
15

blitting-openfl

Rendering engine and lifecycle pipelines for OpenFL
Haxe
7
star
16

storybook-for-pixi.js

Using Storybook for Pixi.js graphics development
JavaScript
5
star
17

blitting

Rendering engine and lifecycle pipelines
ActionScript
5
star
18

pixi-gauges

Pixi.js gauge visualization components
TypeScript
5
star
19

simplegui-openfl

OpenFL port of SimpleGUI by Justin Windle, built upon MinimalComps
Haxe
3
star
20

Blazor-MAUI-Shared-Components

Reusing shared components between Blazor Web and MAUI apps using a Razor Class Library
HTML
3
star
21

scenekit-roxik-sharikura

Recreation of Roxik Sharikura performance example in SceneKit
Swift
3
star
22

openfl-away3d-actuate-demo

3D tweening demo using OpenFL, Away3D, Actuate, and Haxe
Haxe
3
star
23

threejs-roxik-sharikura

Recreation of Roxik Sharikura performance example in Three.js
JavaScript
2
star
24

storybook-for-threejs

Using Storybook for Three.js graphics development
JavaScript
2
star
25

deck-shuffle

JavaScript
1
star
26

pixi-experiments

Pixi.js experimental prototypes
JavaScript
1
star
27

material-ui-5-boilerplate

Material UI 5 React boilerplate with Redux Toolkit
JavaScript
1
star
28

pixi-rulers

Pixi.js rulers for measurement
JavaScript
1
star
29

unity-roxik-sharikura

Recreation of Roxik Sharikura performance example in Unity 3D
C#
1
star
30

openfl-away3d-roxik-sharikura

Recreation of Roxik Sharikura performance example
Haxe
1
star
31

jasonsturges.github.io

Personal website of Jason Sturges
HTML
1
star
32

jsbench.io

Curated JavaScript performance benchmarks
JavaScript
1
star
33

apple-news-compiler

Apple News format compiler for optimized JSON article
JavaScript
1
star
34

pixi-visualization

Experimental Lerna monorepo of pixi-based packages
TypeScript
1
star
35

sort-contiguous

Contiguous list sorting, similar to macOS Finder sort
TypeScript
1
star
36

babylon-roxik-sharikura

Recreation of Roxik Sharikura performance example in Babylon.js
JavaScript
1
star