Skip to content
Docs Try Aspire
Docs Try

EFMigrationResourceBuilderExtensions Methods

Class Methods 7 members
Extension methods for configuring EF Core migration resources.
PublishAsMigrationBundle(IResourceBuilder<EFMigrationResource>, string?, bool, bool, string?) Section titled PublishAsMigrationBundle(IResourceBuilder<EFMigrationResource>, string?, bool, bool, string?) extension IResourceBuilder<EFMigrationResource>
Configures the EF migration resource to generate a migration bundle during publishing.
public static class EFMigrationResourceBuilderExtensions
{
public static IResourceBuilder<EFMigrationResource> PublishAsMigrationBundle(
this IResourceBuilder<EFMigrationResource> builder,
string? targetRuntime = null,
bool selfContained = false,
bool publishContainer = false,
string? baseImage = null)
{
// ...
}
}
builder IResourceBuilder<EFMigrationResource> The resource builder.
targetRuntime string? optional The target runtime identifier for the bundle (e.g., linux-x64, win-x64). If null and publishContainer is true, defaults to linux-x64 so the bundle can run inside a Linux container image. When publishContainer is false the current runtime is used.
selfContained bool optional If true, creates a self-contained bundle that includes the .NET runtime. Never defaulted by publishContainer — user-specified value is always respected.
publishContainer bool optional If true, the bundle is published as a container image that applies migrations at deploy time. The resource becomes a compute resource; each target environment deploys it the same way it deploys any other container (supplying connection strings from referenced ApplicationModel.IResourceWithConnectionString dependencies via the standard WithReference mechanism).
baseImage string? optional Overrides the base container image for the generated Dockerfile. When null (the default), the image is derived from the project's target framework — for example, mcr.microsoft.com/dotnet/runtime:10.0 for a net10.0 framework-dependent bundle. Set this when the default is not suitable, e.g. for preview SDKs or custom base images. Only meaningful when publishContainer is true.
IResourceBuilder<EFMigrationResource> The resource builder for chaining.

During aspire publish, the bundle executable is written to the publish output directory under the efmigrations folder. When publishContainer is true, Aspire also generates a Dockerfile that packages the bundle into a container image; the container reads the connection string from a ConnectionStrings__<name> environment variable provided by the referenced database resource (call .WithReference(db) on the migration builder, or the connection string is injected automatically for every ApplicationModel.IResourceWithConnectionString that the migration resource .WaitFor s).

PublishAsMigrationScript(IResourceBuilder<EFMigrationResource>, bool, bool) Section titled PublishAsMigrationScript(IResourceBuilder<EFMigrationResource>, bool, bool) extension IResourceBuilder<EFMigrationResource>
Configures the EF migration resource to generate a migration script during publishing.
public static class EFMigrationResourceBuilderExtensions
{
public static IResourceBuilder<EFMigrationResource> PublishAsMigrationScript(
this IResourceBuilder<EFMigrationResource> builder,
bool idempotent = true,
bool noTransactions = false)
{
// ...
}
}
builder IResourceBuilder<EFMigrationResource> The resource builder.
idempotent bool optional If true (the default), generates an idempotent script with IF NOT EXISTS checks so it can be safely re-run against a database that has already had some or all of the migrations applied.
noTransactions bool optional If true, omits transaction statements from the script.
IResourceBuilder<EFMigrationResource> The resource builder for chaining.
During aspire publish, the generated SQL script is written to the publish output directory under the efmigrations folder. The script is included as a deployment artifact, but it is not executed automatically during deployment.
RunDatabaseUpdateOnStart(IResourceBuilder<EFMigrationResource>) Section titled RunDatabaseUpdateOnStart(IResourceBuilder<EFMigrationResource>) extension IResourceBuilder<EFMigrationResource>
Configures the EF migration resource to run database update when the AppHost starts.
public static class EFMigrationResourceBuilderExtensions
{
public static IResourceBuilder<EFMigrationResource> RunDatabaseUpdateOnStart(
this IResourceBuilder<EFMigrationResource> builder)
{
// ...
}
}
builder IResourceBuilder<EFMigrationResource> The resource builder.
IResourceBuilder<EFMigrationResource> The resource builder for chaining.

When enabled, migrations will be applied during AppHost startup. This only affects local run-mode execution. The migrations resource is not deployed with the app, so the command has no effect during publish or deployment.

A health check is automatically registered for this resource, allowing other resources to use .WaitFor() to wait until migrations complete before starting.

WithMigrationNamespace(IResourceBuilder<EFMigrationResource>, string) Section titled WithMigrationNamespace(IResourceBuilder<EFMigrationResource>, string) extension IResourceBuilder<EFMigrationResource>
Configures the namespace for new migrations created with the Add Migration command.
public static class EFMigrationResourceBuilderExtensions
{
public static IResourceBuilder<EFMigrationResource> WithMigrationNamespace(
this IResourceBuilder<EFMigrationResource> builder,
string @namespace)
{
// ...
}
}
builder IResourceBuilder<EFMigrationResource> The resource builder.
namespace string The namespace for generated migrations.
IResourceBuilder<EFMigrationResource> The resource builder for chaining.
If not specified, the namespace will be derived from the project's default namespace. Example: "MyApp.Data.Migrations" or "MyApp.Infrastructure.Migrations".
WithMigrationOutputDirectory(IResourceBuilder<EFMigrationResource>, string) Section titled WithMigrationOutputDirectory(IResourceBuilder<EFMigrationResource>, string) extension IResourceBuilder<EFMigrationResource>
Configures the output directory for new migrations created with the Add Migration command.
public static class EFMigrationResourceBuilderExtensions
{
public static IResourceBuilder<EFMigrationResource> WithMigrationOutputDirectory(
this IResourceBuilder<EFMigrationResource> builder,
string outputDirectory)
{
// ...
}
}
builder IResourceBuilder<EFMigrationResource> The resource builder.
outputDirectory string The output directory path relative to the project root.
IResourceBuilder<EFMigrationResource> The resource builder for chaining.
If not specified, migrations will be placed in the default 'Migrations' directory. Example: "Data/Migrations" or "Infrastructure/Migrations".
WithMigrationsProject(IResourceBuilder<EFMigrationResource>, string) Section titled WithMigrationsProject(IResourceBuilder<EFMigrationResource>, string) extension IResourceBuilder<EFMigrationResource>
Configures a separate project containing the migrations using a project path.
public static class EFMigrationResourceBuilderExtensions
{
public static IResourceBuilder<EFMigrationResource> WithMigrationsProject(
this IResourceBuilder<EFMigrationResource> builder,
string projectPath)
{
// ...
}
}
builder IResourceBuilder<EFMigrationResource> The resource builder.
projectPath string The path to the project file containing the migrations.
IResourceBuilder<EFMigrationResource> The resource builder for chaining.

Use this method when the migrations are in a different project than the startup project. The target project's path will be used for migration operations while the startup project remains the original project.

WithMigrationsProject(IResourceBuilder<EFMigrationResource>) Section titled WithMigrationsProject(IResourceBuilder<EFMigrationResource>) extension IResourceBuilder<EFMigrationResource>
Configures a separate project containing the migrations using a project metadata type.
public static class EFMigrationResourceBuilderExtensions
{
public static IResourceBuilder<EFMigrationResource> WithMigrationsProject<TProject>(
this IResourceBuilder<EFMigrationResource> builder)
{
// ...
}
}
builder IResourceBuilder<EFMigrationResource> The resource builder.
IResourceBuilder<EFMigrationResource> The resource builder for chaining.

Use this method when the migrations are in a different project than the startup project. The target project's path will be used for migration operations while the startup project remains the original project.

var migrations = project.AddEFMigrations<MyDbContext>("migrations")
.WithMigrationsProject<Projects.MyMigrationsProject>();