Quidditch - Powered By PostgreSQL and ASP.NET The article describes how to build a persistent data store for a .NET web API using PostgreSQL and Entity Framework. The author explains the process of installing PostgreSQL, creating a database, and configuring Entity Framework to connect the database to an ASP.NET application. The project is based on a Harry Potter-inspired road trip game called "Quidditch," for which the author created a score tracker app. As a software developer, it's important to work on your craft. That's why I've been studying ASP.NET for the past few months. In my last article https://nolanmiller.me/posts/intro-to-.net-apis-for-a-javascript-developer/ I walk through how to set up an ASP.NET Minimal Web API. Today, I'll be building on that foundation, by adding a persistent data store, a la PostgreSQL. In order to configure a .NET API to communicate with a PostgreSQL database, I'm going to use Entity Framework. Though I could have worked with my animal sounds API, I decided to implement the database for an app that scores a road trip game that my wife and I took this summer. I recently saw a video about a new road trip game that's inspired by Harry Potter my favorite book series . I knew that my wife and I had to try it. If you're interested, the rules of the game are described in this video, https://www.instagram.com/reel/DEs-Stox4d2/?hl=en and you can play using my score tracker at https://quidditchtrip.nolanmiller.me https://quidditchtrip.nolanmiller.me . In this article, I'll go over making sure that you have PostgreSQL installed and running, and then how to install and configure Entity Framework to read to and write from the database. What you'll need to start: - A .NET Web API that you've created using the dotnet CLI https://nolanmiller.me/posts/supercharge-your-productivity-with-the-.net-cli/ - Access to an internet connection - A little bit of patience Install Postgres There are a few different methods to installing and starting a Postgres server on your machine, but the easiest route is downloading Postgres.app, a MacOS postgres server interface that comes bundled with the psql CLI. If you're not on Mac, or if you're interested in other installation methods the official Postgres website https://www.postgresql.org/download/ has installation instructions for every operating system and includes instructions for other installation methods. Once you're done you should be able to start your Postgres server and also connect to it using the psql command line interface. Create a database Before we can start creating our models and writing data, we need to make sure that we have a database for our application. To do this, connect to psql using the following command: psql -U postgres This will connect us to the Postgres server with the default user postgres . If it prompts you for a password, it should be the default password, postgres . Once you're connected you should see a prompt that ends with and we can issue the following SQL command: postgres= CREATE DATABASE Quidditch; Since this database is for my Quidditch app, I'll name it Quidditch , just replace this with whatever name makes sense for your application. Connect to your database With a database set up, it can be connected to our .NET application using Entity Framework, an object-relational mapper for the .NET ecosystem. EF will automate reading and writing data on our database, and handle table creation and modifications through the development process. To get started with EF for Postgres, install two NuGet packages by running the following commands from a terminal in your .NET project: dotnet add package Microsoft.EntityFrameworkCore.PostgreSQL dotnet add package Microsoft.EntityFrameworkCore.Design To interact with a database we have to create a database context to allow us to interface with the database, configure the context object to use the proper connection string, set up models and create a migration using EF that will scaffold our database. 1. Database Context To create a database context, create a class that inherits from EF's DbContext class, like so: using Microsoft.EntityFrameworkCore; public class QuidditchContext : DbContext { public QuidditchContext DbConextOptions