![]() |
VOOZH | about |
dotnet add package Aspire.Hosting.EntityFrameworkCore --version 13.4.6-preview.1.26319.6
NuGet\Install-Package Aspire.Hosting.EntityFrameworkCore -Version 13.4.6-preview.1.26319.6
<PackageReference Include="Aspire.Hosting.EntityFrameworkCore" Version="13.4.6-preview.1.26319.6" />
<PackageVersion Include="Aspire.Hosting.EntityFrameworkCore" Version="13.4.6-preview.1.26319.6" />Directory.Packages.props
<PackageReference Include="Aspire.Hosting.EntityFrameworkCore" />Project file
paket add Aspire.Hosting.EntityFrameworkCore --version 13.4.6-preview.1.26319.6
#r "nuget: Aspire.Hosting.EntityFrameworkCore, 13.4.6-preview.1.26319.6"
#:package Aspire.Hosting.EntityFrameworkCore@13.4.6-preview.1.26319.6
#addin nuget:?package=Aspire.Hosting.EntityFrameworkCore&version=13.4.6-preview.1.26319.6&prereleaseInstall as a Cake Addin
#tool nuget:?package=Aspire.Hosting.EntityFrameworkCore&version=13.4.6-preview.1.26319.6&prereleaseInstall as a Cake Tool
Provides extension methods and resource definitions for an Aspire AppHost to configure Entity Framework Core migration management.
The target project must reference Microsoft.EntityFrameworkCore.Design. Add the following to your project file:
<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="x.y.z" />
</ItemGroup>
Note: Using dotnet add package will add the reference with PrivateAssets="All" which may not work correctly with the migration commands.
In your AppHost project, install the Aspire EntityFrameworkCore Hosting library with NuGet:
dotnet add package Aspire.Hosting.EntityFrameworkCore
Then, in the AppHost.cs file of AppHost, add EF migrations to a project resource:
var api = builder.AddProject<Projects.Api>("api");
// Add EF migrations for a specific DbContext
var apiMigrations = api.AddEFMigrations("api-migrations", "MyApp.Data.MyDbContext");
When AddEFMigrations is called, the migration resource appears in the Aspire Dashboard with the following commands:
| Command | Description |
|---|---|
| Update Database | Apply pending migrations to the database |
| Drop Database | Delete the database (requires confirmation) |
| Reset Database | Drop and recreate the database with all migrations (requires confirmation) |
| Add Migration... | Create a new migration |
| Remove Migration | Remove the last migration |
| Get Database Status | Show the current migration status |
Note: After adding or removing a migration, all commands are disabled until the target project is recompiled. This prevents executing commands against stale assemblies.
The dotnet-ef tool is automatically downloaded and executed using dotnet tool exec when commands are run. You don't need to install it globally or in a local tool manifest.
You can customize the dotnet-ef tool version, NuGet sources, or allow prerelease versions:
var api = builder.AddProject<Projects.Api>("api");
// Use a specific version of dotnet-ef
var apiMigrations = api.AddEFMigrations("api-migrations", "MyApp.Data.MyDbContext",
configureToolResource: tool =>
{
tool.WithToolVersion("10.0.0");
});
// Allow prerelease versions
var apiMigrations = api.AddEFMigrations("api-migrations", "MyApp.Data.MyDbContext",
configureToolResource: tool =>
{
tool.WithToolPrerelease();
});
// Use a custom NuGet source
var apiMigrations = api.AddEFMigrations("api-migrations", "MyApp.Data.MyDbContext",
configureToolResource: tool =>
{
tool.WithToolSource("https://api.nuget.org/v3/index.json")
.WithToolSource("https://my-feed.example.com/v3/index.json");
});
You can configure migrations to run automatically when the AppHost starts:
var api = builder.AddProject<Projects.Api>("api");
// Add EF migrations and run on startup
var apiMigrations = api.AddEFMigrations("api-migrations", "MyApp.Data.MyDbContext")
.RunDatabaseUpdateOnStart();
// Other resources can wait for migrations to complete
var worker = builder.AddProject<Projects.Worker>("worker")
.WaitForCompletion(apiMigrations);
When RunDatabaseUpdateOnStart() is called, a health check is automatically registered for the migration resource. This enables other resources to use .WaitFor() to wait until migrations complete before starting. The resource transitions through the following states:
Configure where new migrations are created using the Add Migration command:
var apiMigrations = api.AddEFMigrations("api-migrations", "MyApp.Data.MyDbContext")
.WithMigrationOutputDirectory("Data/Migrations") // Custom output directory
.WithMigrationNamespace("MyApp.Data.Migrations"); // Custom namespace
When migrations are in a different project than the startup project, use WithMigrationsProject:
var startup = builder.AddProject<Projects.Api>("api");
// Using a project metadata type (recommended)
var apiMigrations = startup.AddEFMigrations("api-migrations", "MyApp.Data.MyDbContext")
.WithMigrationsProject<Projects.Data>();
// Or using a project path
var apiMigrations = startup.AddEFMigrations("api-migrations", "MyApp.Data.MyDbContext")
.WithMigrationsProject("../MyApp.Data/MyApp.Data.csproj");
You can add migrations for multiple DbContexts in the same project:
var api = builder.AddProject<Projects.Api>("api");
var userMigrations = api.AddEFMigrations("user-migrations", "MyApp.Data.UserDbContext");
var orderMigrations = api.AddEFMigrations("order-migrations", "MyApp.Data.OrderDbContext");
Configure migration script or bundle generation during publishing:
// Generate a SQL migration script during publish
var apiMigrations = api.AddEFMigrations("api-migrations", "MyApp.Data.MyDbContext")
.PublishAsMigrationScript();
// Or generate a self-contained migration bundle executable
var apiMigrations = api.AddEFMigrations("api-migrations", "MyApp.Data.MyDbContext")
.PublishAsMigrationBundle();
When publishing, these methods add pipeline steps that run during aspire publish and write
their artifacts into the publish output directory under efmigrations/.
Passing publishContainer: true to PublishAsMigrationBundle tells Aspire to also wrap the
generated bundle in a container image. The migration resource becomes a compute resource that
each compute environment (Docker Compose, Azure Container Apps, Kubernetes, Azure App Service,
Azure Functions, etc.) deploys exactly like any other container you add to the AppHost.
var db = builder.AddPostgres("pg").AddDatabase("appdb");
var api = builder.AddProject<Projects.Api>("api").WithReference(db);
var apiMigrations = api.AddEFMigrations("api-migrations", "MyApp.Data.MyDbContext")
.WaitFor(db) // required — see below
.PublishAsMigrationBundle(publishContainer: true);
Dockerfile uses mcr.microsoft.com/dotnet/runtime:10.0 (or
mcr.microsoft.com/dotnet/runtime-deps:10.0 when selfContained: true).targetRuntime argument defaults to linux-x64 when publishContainer: true; override it
explicitly to publish for a different architecture (e.g., linux-arm64).IResourceWithConnectionString that you pass to .WaitFor(...) on the
migration resource — the same way it does for any other compute resource with a database dependency..WaitFor(database) is required; the bundle cannot run without a connection string.aspire run) is unaffected — no container image is built locally. The migration
resource still appears in the dashboard with its tool commands (Update Database, etc.). Container
wiring only activates under aspire publish.A migration bundle is idempotent — running it twice is safe, the second run is a no-op — but
different compute environments have different "run to completion" mechanisms, so there is no
single environment-agnostic flag that means "don't restart after the container exits". Use the
appropriate environment-specific helper after PublishAsMigrationBundle(publishContainer: true)
to avoid the container being restarted after it finishes:
Azure Container Apps — publish as a manually-triggered Azure Container App Job:
var apiMigrations = api.AddEFMigrations("api-migrations", "MyApp.Data.MyDbContext")
.WaitFor(db)
.PublishAsMigrationBundle(publishContainer: true)
.PublishAsAzureContainerAppJob(); // requires Aspire.Hosting.Azure.AppContainers
The container app job runs once per manual invocation and stops.
Docker Compose — tell Compose not to restart the container after it exits:
var apiMigrations = api.AddEFMigrations("api-migrations", "MyApp.Data.MyDbContext")
.WaitFor(db)
.PublishAsMigrationBundle(publishContainer: true)
.PublishAsDockerComposeService((_, service) => service.Restart = "no"); // requires Aspire.Hosting.Docker
The bundle runs to completion during docker compose up and then stops; it will only run again
on subsequent up calls, which is safe because the bundle is idempotent.
Kubernetes — customize the generated manifest to a Job or set restartPolicy: OnFailure
using your Kubernetes publisher of choice (for example via AddKubernetesEnvironment() and the
matching customization hook).
Azure App Service / Azure Functions — App Service and Functions keep compute instances warm, so the preferred pattern is not to deploy the bundle as part of those environments. Instead, run the bundle as a pre-deployment or init step — see "Generate the bundle and execute it yourself" below.
If you only want the aspire publish step to produce the bundle artifact (for example to run it
from a deployment pipeline, a database-administrator's shell, or a CI job), omit
publishContainer: true:
var apiMigrations = api.AddEFMigrations("api-migrations", "MyApp.Data.MyDbContext")
.PublishAsMigrationBundle(); // artifact only, no Dockerfile
This writes the platform-native executable to <publish-output>/efmigrations/<name>[.exe] and
does not add any compute resource to the deployment manifest. Execute the bundle when appropriate
for your deployment flow:
./<publish-output>/efmigrations/api-migrations --connection "$CONNECTION_STRING" --verbove
Similarly you can use PublishAsMigrationScript() if you also want a raw SQL script produced.
https://learn.microsoft.com/dotnet/aspire/ https://learn.microsoft.com/ef/core/managing-schemas/migrations/
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net8.0 net8.0 is compatible. net8.0-android net8.0-android was computed. net8.0-browser net8.0-browser was computed. net8.0-ios net8.0-ios was computed. net8.0-maccatalyst net8.0-maccatalyst was computed. net8.0-macos net8.0-macos was computed. net8.0-tvos net8.0-tvos was computed. net8.0-windows net8.0-windows was computed. net9.0 net9.0 was computed. net9.0-android net9.0-android was computed. net9.0-browser net9.0-browser was computed. net9.0-ios net9.0-ios was computed. net9.0-maccatalyst net9.0-maccatalyst was computed. net9.0-macos net9.0-macos was computed. net9.0-tvos net9.0-tvos was computed. net9.0-windows net9.0-windows was computed. net10.0 net10.0 was computed. net10.0-android net10.0-android was computed. net10.0-browser net10.0-browser was computed. net10.0-ios net10.0-ios was computed. net10.0-maccatalyst net10.0-maccatalyst was computed. net10.0-macos net10.0-macos was computed. net10.0-tvos net10.0-tvos was computed. net10.0-windows net10.0-windows was computed. |
This package is not used by any NuGet packages.
This package is not used by any popular GitHub repositories.
| Version | Downloads | Last Updated |
|---|---|---|
| 13.4.6-preview.1.26319.6 | 286 | 6/19/2026 |
| 13.4.5-preview.1.26316.12 | 410 | 6/17/2026 |
| 13.4.4-preview.1.26314.3 | 210 | 6/15/2026 |
| 13.4.3-preview.1.26305.13 | 433 | 6/8/2026 |
| 13.4.2-preview.1.26303.6 | 573 | 6/3/2026 |
| 13.4.1-preview.1.26303.3 | 70 | 6/3/2026 |
| 13.4.0-preview.1.26281.18 | 806 | 6/1/2026 |
| 13.3.5-preview.1.26270.6 | 2,985 | 5/21/2026 |
| 13.3.4-preview.1.26268.8 | 98 | 5/19/2026 |
| 13.3.3-preview.1.26264.13 | 1,452 | 5/15/2026 |
| 13.3.2-preview.1.26263.11 | 111 | 5/14/2026 |
| 13.3.1-preview.1.26261.7 | 552 | 5/12/2026 |
| 13.3.0-preview.1.26256.5 | 400 | 5/7/2026 |