.NET and ASP.NET Core 10 cheatsheet

.NET and ASP.NET Core 10: Entity Framework Core

6 common .NET and ASP.NET Core 10 tasks.

Install the EF tool

Install the dotnet ef command-line tool globally.

dotnet tool install --global dotnet-ef

Add a migration

Create a migration from model changes.

dotnet ef migrations add InitialCreate

Apply migrations

Update the database to the latest migration.

dotnet ef database update

Caution: Check the migration and back up production data first.

Remove the last migration

Delete the most recent migration, if it has not been applied.

dotnet ef migrations remove

Generate a SQL script

Write the SQL for all migrations to a file, for review or deployment.

dotnet ef migrations script -o migrate.sql

Register a DbContext

Add Entity Framework Core with PostgreSQL to dependency injection.

builder.Services.AddDbContext<AppDbContext>(options =>
    options.UseNpgsql(builder.Configuration.GetConnectionString("Default")));

Was this page helpful?