Clone, run, and explore this sample
This sample demonstrates how to use Entity Framework Core's migrations feature with Aspire.
The entry point that composes every resource and dependency in this sample's distributed application.
var builder = DistributedApplication.CreateBuilder(args);
var sqlserver = builder.AddSqlServer("sqlserver") .WithDataVolume() .WithLifetime(ContainerLifetime.Persistent);
var db1 = sqlserver.AddDatabase("db1");
var migrationService = builder.AddProject<Projects.DatabaseMigrations_MigrationService>("migration") .WithReference(db1) .WaitFor(db1);
builder.AddProject<Projects.DatabaseMigrations_ApiService>("api") .WithReference(db1) .WaitForCompletion(migrationService);
builder.Build().Run();The sample has three important projects:
DatabaseMigrations.ApiService- A web app that uses the database.DatabaseMigrations.MigrationService- A background worker app that applies migrations when it starts up.DatabaseMigrations.ApiModel- The EF Core context and entity types. This project is used by both the API and migration service.
DatabaseMigrations.ApiService and DatabaseMigrations.MigrationService reference a SQL Server resource. During local development the SQL Server resource is launched in a container.
Demonstrates
Section titled Demonstrates- How to create migrations in an Aspire solution
- How to apply migrations in an Aspire solution
Sample prerequisites
Section titled Sample prerequisitesThis sample is written in C# and targets .NET 10. It requires the .NET 10.0 SDK or later.
The
dotnet ef.NET tool is required. It can be installed by running the following in a terminal:Terminal window dotnet tool install --global dotnet-ef
Create migration
Section titled Create migrationThe DatabaseMigrations.MigrationService project contains the EF Core migrations. The `dotnet ef` command-line tool can be used to create new migrations:
Update the
Entryentity in database context inMyDb1Context.cs. Add aNameproperty:public class Entry{public Guid Id { get; set; } = Guid.NewGuid();public string? Name { get; set; }}Open a command prompt in the
DatabaseMigrations.MigrationServicedirectory and run the EF Core migration tool to create a migration named MyNewMigration.Terminal window dotnet ef migrations add MyNewMigrationThe preceding command:
- Runs EF Core migration command-line tool in the
DatabaseMigrations.MigrationServicedirectory.dotnet efis run in this location because it will be used as the default target project for the new migration and the tool will run the startup code inProgram.csto find and configure the context to be used.
- Creates the migration named
MyNewMigrationin theDatabaseMigrations.MigrationServiceproject.
- Runs EF Core migration command-line tool in the
View the new migration files in the
DatabaseMigrations.ApiModelproject.
Run the app
Section titled Run the appIf using the Aspire CLI, run aspire run from this directory.
If using VS Code, open this directory as a workspace and launch the DatabaseMigrations.AppHost project using either the Aspire or C# debuggers.
If using Visual Studio, open the solution file DatabaseMigrations.slnx and launch/debug the DatabaseMigrations.AppHost project.
If using the .NET CLI, run dotnet run from the DatabaseMigrations.AppHost directory.
When the app starts up, the DatabaseMigrations.MigrationService background worker runs migrations on the SQL Server container. The migration service:
- Creates a database in the SQL Server container.
- Creates the database schema.
- Stops itself once the migration is complete.