# EFMigrationResourceBuilderExtensions Methods

- Package: [Aspire.Hosting.EntityFrameworkCore](/reference/api/csharp/aspire.hosting.entityframeworkcore.md)
- Type: [EFMigrationResourceBuilderExtensions](/reference/api/csharp/aspire.hosting.entityframeworkcore/efmigrationresourcebuilderextensions.md)
- Kind: `Methods`
- Members: `7`

Extension methods for configuring EF Core migration resources.

## PublishAsMigrationBundle(IResourceBuilder<EFMigrationResource>, string?, bool, bool, string?)

- Name: `PublishAsMigrationBundle(IResourceBuilder<EFMigrationResource>, string?, bool, bool, string?)`
- Modifiers: `extension`
- Returns: `IResourceBuilder<EFMigrationResource>`
- Source: [GitHub](https://github.com/microsoft/aspire/blob/5bd693ae1897dee5e2ce71c2cc08879c1c7eff51/src/Aspire.Hosting.EntityFrameworkCore/EFMigrationResourceBuilderExtensions.cs#L154-L177)

Configures the EF migration resource to generate a migration bundle during publishing.

```csharp
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)
    {
        // ...
    }
}
```

## Parameters

- `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`.

## Returns

`IResourceBuilder<EFMigrationResource>` -- The resource builder for chaining.

## Remarks

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).

## ATS metadata

### ATS export

- Available to Polyglot AppHosts through the Aspire Type System.

## PublishAsMigrationScript(IResourceBuilder<EFMigrationResource>, bool, bool)

- Name: `PublishAsMigrationScript(IResourceBuilder<EFMigrationResource>, bool, bool)`
- Modifiers: `extension`
- Returns: `IResourceBuilder<EFMigrationResource>`
- Source: [GitHub](https://github.com/microsoft/aspire/blob/5bd693ae1897dee5e2ce71c2cc08879c1c7eff51/src/Aspire.Hosting.EntityFrameworkCore/EFMigrationResourceBuilderExtensions.cs#L103-L106)

Configures the EF migration resource to generate a migration script during publishing.

```csharp
public static class EFMigrationResourceBuilderExtensions
{
    public static IResourceBuilder<EFMigrationResource> PublishAsMigrationScript(
        this IResourceBuilder<EFMigrationResource> builder,
        bool idempotent = true,
        bool noTransactions = false)
    {
        // ...
    }
}
```

## Parameters

- `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.

## Returns

`IResourceBuilder<EFMigrationResource>` -- The resource builder for chaining.

## Remarks

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.

## ATS metadata

### ATS export

- Available to Polyglot AppHosts through the Aspire Type System.

## RunDatabaseUpdateOnStart(IResourceBuilder<EFMigrationResource>)

- Name: `RunDatabaseUpdateOnStart(IResourceBuilder<EFMigrationResource>)`
- Modifiers: `extension`
- Returns: `IResourceBuilder<EFMigrationResource>`
- Source: [GitHub](https://github.com/microsoft/aspire/blob/5bd693ae1897dee5e2ce71c2cc08879c1c7eff51/src/Aspire.Hosting.EntityFrameworkCore/EFMigrationResourceBuilderExtensions.cs#L38-L46)

Configures the EF migration resource to run database update when the AppHost starts.

```csharp
public static class EFMigrationResourceBuilderExtensions
{
    public static IResourceBuilder<EFMigrationResource> RunDatabaseUpdateOnStart(
        this IResourceBuilder<EFMigrationResource> builder)
    {
        // ...
    }
}
```

## Parameters

- `builder` (`IResourceBuilder<EFMigrationResource>`)
  The resource builder.

## Returns

`IResourceBuilder<EFMigrationResource>` -- The resource builder for chaining.

## Remarks

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.

## ATS metadata

### ATS export

- Available to Polyglot AppHosts through the Aspire Type System.

## WithMigrationNamespace(IResourceBuilder<EFMigrationResource>, string)

- Name: `WithMigrationNamespace(IResourceBuilder<EFMigrationResource>, string)`
- Modifiers: `extension`
- Returns: `IResourceBuilder<EFMigrationResource>`
- Source: [GitHub](https://github.com/microsoft/aspire/blob/5bd693ae1897dee5e2ce71c2cc08879c1c7eff51/src/Aspire.Hosting.EntityFrameworkCore/EFMigrationResourceBuilderExtensions.cs#L211-L213)

Configures the namespace for new migrations created with the Add Migration command.

```csharp
public static class EFMigrationResourceBuilderExtensions
{
    public static IResourceBuilder<EFMigrationResource> WithMigrationNamespace(
        this IResourceBuilder<EFMigrationResource> builder,
        string @namespace)
    {
        // ...
    }
}
```

## Parameters

- `builder` (`IResourceBuilder<EFMigrationResource>`)
  The resource builder.
- `namespace` (`string`)
  The namespace for generated migrations.

## Returns

`IResourceBuilder<EFMigrationResource>` -- The resource builder for chaining.

## Remarks

If not specified, the namespace will be derived from the project's default namespace. Example: "MyApp.Data.Migrations" or "MyApp.Infrastructure.Migrations".

## ATS metadata

### ATS export

- Available to Polyglot AppHosts through the Aspire Type System.

## WithMigrationOutputDirectory(IResourceBuilder<EFMigrationResource>, string)

- Name: `WithMigrationOutputDirectory(IResourceBuilder<EFMigrationResource>, string)`
- Modifiers: `extension`
- Returns: `IResourceBuilder<EFMigrationResource>`
- Source: [GitHub](https://github.com/microsoft/aspire/blob/5bd693ae1897dee5e2ce71c2cc08879c1c7eff51/src/Aspire.Hosting.EntityFrameworkCore/EFMigrationResourceBuilderExtensions.cs#L193-L195)

Configures the output directory for new migrations created with the Add Migration command.

```csharp
public static class EFMigrationResourceBuilderExtensions
{
    public static IResourceBuilder<EFMigrationResource> WithMigrationOutputDirectory(
        this IResourceBuilder<EFMigrationResource> builder,
        string outputDirectory)
    {
        // ...
    }
}
```

## Parameters

- `builder` (`IResourceBuilder<EFMigrationResource>`)
  The resource builder.
- `outputDirectory` (`string`)
  The output directory path relative to the project root.

## Returns

`IResourceBuilder<EFMigrationResource>` -- The resource builder for chaining.

## Remarks

If not specified, migrations will be placed in the default 'Migrations' directory. Example: "Data/Migrations" or "Infrastructure/Migrations".

## ATS metadata

### ATS export

- Available to Polyglot AppHosts through the Aspire Type System.

## WithMigrationsProject(IResourceBuilder<EFMigrationResource>, string)

- Name: `WithMigrationsProject(IResourceBuilder<EFMigrationResource>, string)`
- Modifiers: `extension`
- Returns: `IResourceBuilder<EFMigrationResource>`
- Source: [GitHub](https://github.com/microsoft/aspire/blob/5bd693ae1897dee5e2ce71c2cc08879c1c7eff51/src/Aspire.Hosting.EntityFrameworkCore/EFMigrationResourceBuilderExtensions.cs#L232-L236)

Configures a separate project containing the migrations using a project path.

```csharp
public static class EFMigrationResourceBuilderExtensions
{
    public static IResourceBuilder<EFMigrationResource> WithMigrationsProject(
        this IResourceBuilder<EFMigrationResource> builder,
        string projectPath)
    {
        // ...
    }
}
```

## Parameters

- `builder` (`IResourceBuilder<EFMigrationResource>`)
  The resource builder.
- `projectPath` (`string`)
  The path to the project file containing the migrations.

## Returns

`IResourceBuilder<EFMigrationResource>` -- The resource builder for chaining.

## Remarks

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.

## ATS metadata

### ATS export

- Available to Polyglot AppHosts through the Aspire Type System.

## WithMigrationsProject(IResourceBuilder<EFMigrationResource>)

- Name: `WithMigrationsProject(IResourceBuilder<EFMigrationResource>)`
- Modifiers: `extension`
- Returns: `IResourceBuilder<EFMigrationResource>`
- Source: [GitHub](https://github.com/microsoft/aspire/blob/5bd693ae1897dee5e2ce71c2cc08879c1c7eff51/src/Aspire.Hosting.EntityFrameworkCore/EFMigrationResourceBuilderExtensions.cs#L262-L263)

Configures a separate project containing the migrations using a project metadata type.

```csharp
public static class EFMigrationResourceBuilderExtensions
{
    public static IResourceBuilder<EFMigrationResource> WithMigrationsProject<TProject>(
        this IResourceBuilder<EFMigrationResource> builder)
    {
        // ...
    }
}
```

## Parameters

- `builder` (`IResourceBuilder<EFMigrationResource>`)
  The resource builder.

## Returns

`IResourceBuilder<EFMigrationResource>` -- The resource builder for chaining.

## Remarks

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.

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

## ATS metadata

### ATS export

- Available to Polyglot AppHosts through the Aspire Type System.
