Schema migration with Neon Postgres and Entity Framework
Set up Neon Postgres and run migrations for your Entity Framework project
Entity Framework is a popular Object-Relational Mapping (ORM) framework for .NET applications. It simplifies database access by allowing developers to work with domain-specific objects and properties without focusing on the underlying database tables and columns. Entity Framework also provides a powerful migration system that enables you to define and manage database schema changes over time.
This guide demonstrates how to use Entity Framework with the Neon Postgres database. We'll create a simple .NET application and walk through the process of setting up the database, defining models, and generating and running migrations to manage schema changes.
Prerequisites
To follow along with this guide, you will need:
- A Neon account. If you do not have one, sign up at Neon. Your Neon project comes with a ready-to-use Postgres database named
neondb
. We'll use this database in the following examples. - A recent version of the .NET SDK installed on your local machine. This guide uses .NET 8.0, which is the current Long-Term Support (LTS) version.
Setting up your Neon database
Initialize a new project
- Log in to the Neon Console and navigate to the Projects section.
- Select a project or click the New Project button to create a new one.
Retrieve your Neon database connection string
On the Neon project dashboard, navigate to the Connection Details section in your project dashboard to find your database connection URI. It should be in the format below:
The Postgres client library we use in this guide requires the connection string to be in the following format:
Construct the connection string in this format using the correct values for your Neon connection URI. Keep it handy for later use.
Setting up the Entity Framework project
Create a new .NET project
Open your terminal and run the following command to create a new .NET console application:
Install dependencies
Run the following commands to install the necessary NuGet packages:
These packages include the Entity Framework Core libraries, the design-time components for migrations, and the Npgsql provider for PostgreSQL.
We will also need the EF Core
tools to generate and run migrations. Install the dotnet-ef
tool globally:
Set up the database configuration
Create a new file named .env
in the project root directory and add the following configuration:
Replace NEON_POSTGRES_CONNECTION_STRING
with the formatted connection string you constructed earlier.
Defining data models and running migrations
Create the data models
Create a new file named Models.cs
in the project directory and define the data models for your application:
This code defines two entities: Author
and Book
. The Author
entity represents an author with properties for name, bio, and created timestamp. The Book
entity represents a book with properties for title, author (as a foreign key to the Author
entity), and created timestamp.
Also, create a new file named ApplicationDbContext.cs
in the project directory and add the following code:
The ApplicationDbContext
class derives from DbContext
and represents the database context. It includes the method where we configure the database connection and seed the database at initialization. We also set default values for the CreatedAt
properties of the Author
and Book
entities.
Add seeding script
To seed the database with some initial data, create another script named ModelBuilderExtensions.cs
in the project directory and add the following code:
This code defines a static method Seed
that populates the database with some initial authors and books. Entity framework will include this data when generating database migrations.
Generate migration files
To generate migration files based on the defined models, run the following command:
This command detects the new Author
and Book
entities and generates migration files in the Migrations
directory to create the corresponding tables in the database.
Apply the migration
To apply the migration and create the tables in the Neon Postgres database, run the following command:
This command executes the migration file and creates the necessary tables in the database. It will also seed the database with the initial data defined in the Seed
method.
Creating the web application
Implement the API endpoints
The project directory has a Program.cs
file that contains the application entry point. Replace the contents of this file with the following code:
This code sets up a simple web application with two endpoints: /authors
and /books/[authorId]
. The /authors
endpoint returns a list of all authors, while the /books/[authorId]
endpoint returns a list of books written by the author with the specified ID.
Test the application
To test the application, run the following command:
This will start a local web server at http://localhost:5000
. Navigate to these endpoints in your browser to view the seeded data.
Applying schema changes
We'll see how to handle schema changes by adding a new property Country
to the Author
entity to store the author's country of origin.
Update the data model
Open the Models.cs
file and add a new property to the Author
entity:
Also, update the seed data entries for the Author
model to include the Country
property:
Generate and run the migration
To generate a new migration file for the above schema change, run the following command in the terminal:
This command detects the updated Author
entity and generates a new migration file to add the new column to the corresponding table in the database. It will also include upserting the seed data with the new property added.
Now, to apply the migration, run the following command:
Test the schema change
Run the application again:
Now, if you navigate to the /authors
endpoint, you should see the new Country
property included in the response.
Source code
You can find the source code for the application described in this guide on GitHub.
Migrations with Neon and Entity Framework
Run Neon database migrations in an Entity Framework project
Conclusion
In this guide, we demonstrated how to set up an Entity Framework project with Neon Postgres, define data models, generate migrations, and run them. Entity Framework's migration system make it easy to interact with the database and manage schema evolution over time.
Resources
For more information on the tools and concepts used in this guide, refer to the following resources:
Need help?
Join our Discord Server to ask questions or see what others are doing with Neon. Users on paid plans can open a support ticket from the console. For more detail, see Getting Support.
Last updated on