In EF Core 10, what is a migration bundle, how do you build efbundle for a Linux release pipeline, and what does it need when it runs?
answer
- dotnet ef migrations bundle
- --self-contained and -r linux-x64
- equivalent to database update
- --connection and settings beside the file
- environment defaults to Development
basics
~20 sA bundle is an executable, efbundle by default, that does what dotnet ef database update does without the SDK or source. Build it in CI with --self-contained -r linux-x64, then run it with --connection and the right environment.
solid answer
~40 s`dotnet ef migrations bundle` compiles the migrations and the startup project into one executable, named `efbundle` unless `--output` says otherwise; `--self-contained` with `-r linux-x64` (`--target-runtime`) makes it run on a Linux agent without the .NET runtime, and `--force` overwrites an existing file. Running it is equivalent to `dotnet ef database update`: an optional target migration (`0` reverts all), `--connection` to override the configured string, EF Core's migration lock and the synchronous `UseSeeding` delegate. It exits non-zero on failure, so the pipeline stops. It still creates the context the way the design-time tools do, so set `ASPNETCORE_ENVIRONMENT`, because it otherwise assumes `Development`, and copy any `appsettings.json` the context configuration reads next to it. Unlike a script, a bundle cannot show the SQL it will run.
code
bash · 8 lines# CI: build once, for the release machine's runtime
ASPNETCORE_ENVIRONMENT=Production \
dotnet ef migrations bundle --self-contained -r linux-x64 \
--project src/Payroll.Api --output artifacts/efbundle --force
# Release: apply, then let the exit code gate the deploy
ASPNETCORE_ENVIRONMENT=Production \
./artifacts/efbundle --connection "$PAYROLL_MIGRATOR_CONNECTION"go deeper
Know that dotnet ef migrations bundle produces an efbundle executable that applies migrations like database update, without the SDK.
Explain --self-contained and -r, the bundle's target and --connection arguments, its exit code, and why it needs the environment and settings files of the startup project.
Wire it into a release: build once per commit, run as a single gated step with a schema-owning secret, set the environment explicitly, and plan for partial application on failure.
Choose between bundles and reviewed scripts per system, weighing automation, auditability, least privilege and how rollbacks are executed and rehearsed.
## What a bundle is A **migration bundle** is a single executable that applies a project's migrations. It exists because the two older routes each have a gap in automated deployment: `dotnet ef database update` needs the SDK, the dotnet-ef tool and the source on the machine, and an SQL script needs a separate SQL client and gives up EF Core's lock and seeding. A bundle is built once in CI and executed later by the release, with nothing else installed. ## Building it ```bash dotnet ef migrations bundle --self-contained -r linux-x64 \ --project src/Payroll.Api --output artifacts/efbundle --force ``` - `--output` / `-o` sets the file path; the default name is `efbundle` (`efbundle.exe` on Windows). - `--self-contained` includes the .NET runtime, so the target machine needs none. - `--target-runtime` / `-r` sets the runtime identifier of the machine that will run it, which can differ from the build agent's. - `--force` overwrites a bundle that already exists at that path. - The usual project options apply: `--project`, `--startup-project`, `--context`, `--configuration`, and `--framework` for a multi-targeted project. ## Running it The bundle accepts the same target as `database update` plus a few options: | Input | Meaning | |---|---| | `<MIGRATION>` | target migration; omitted means the latest, `0` reverts everything | | `--connection` | connection string; defaults to the one configured in `AddDbContext` or `OnConfiguring` | | `-v` / `--verbose` | verbose output | | `--no-color`, `--prefix-output` | output formatting for log collectors | When it runs, it behaves like `database update`: 1. creates the database and the history table if missing; 2. takes EF Core's migration lock; 3. applies unapplied migrations, each in its own transaction in EF Core 10; 4. runs the synchronous `UseSeeding` delegate; 5. exits with code `0`, or `1` with the error message if anything failed. ## What it needs at run time - **The environment.** The bundle obtains the context the way the design-time tools do: through an `IDesignTimeDbContextFactory<TContext>`, or by running the startup project's `Program.cs` up to the point where the host is built. Like the tools, it uses `Development` when neither `ASPNETCORE_ENVIRONMENT` nor `DOTNET_ENVIRONMENT` is set, which can load development settings and user secrets. Set `ASPNETCORE_ENVIRONMENT=Production` explicitly. - **Configuration files.** If the context configuration reads `appsettings.json`, those files are resolved from the bundle's execution directory, so they must be copied next to it. - **A connection with schema rights.** Pass a deployment connection through `--connection` from the pipeline's secret store rather than baking it into a settings file; the application's own login normally lacks DDL permissions. - **A database that is reachable** before the step starts. ## Using it in a payroll release 1. CI builds the app and the bundle from the same commit and publishes both as artifacts. 2. The release runs the bundle once, as its own step, before the new application version starts. 3. A non-zero exit fails the release and the old version keeps serving. ## Limits - A bundle cannot print the SQL it would execute or list the migrations it contains; when review is mandatory, ship an idempotent script instead. - A bundle is tied to the runtime identifier it was built for. - Earlier migrations of a run stay applied if a later one fails, because EF Core 10 commits each migration separately. ## Bundle, script or tools | Need | Bundle | Idempotent script | `dotnet ef database update` | |---|---|---|---| | Runs without the SDK and source | yes | yes | no | | Exit code a pipeline can gate on | yes | depends on the SQL client | yes | | EF migration lock and `UseSeeding` | yes | no | yes | | Shows the SQL before running | no | yes | no | | Target and rollback by migration name | yes | through `FROM`/`TO` when generating | yes | A bundle is the default the EF Core docs recommend for automated deployment; a script wins whenever the exact SQL must be approved.
- How do you roll a payroll database back with a bundle?Pass the migration that should remain applied: `./efbundle AddPayslipTaxCode --connection "$CONN"` runs `Down` for every newer migration, newest first, and `./efbundle 0` reverts all of them. The bundle must contain the newer migrations to revert them, and their `Down` methods may drop data.
- When is an idempotent script the better artifact than a bundle?When someone must approve the exact SQL, when a DBA or a database tool runs changes, or when the provider is not reachable from the release agent. A bundle cannot show its SQL. A script, in turn, gives up EF Core's migration lock and `UseSeeding`, so each team picks by which guarantee it needs.
- A migration bundle needs the .NET SDK and dotnet-ef on the release machine.
- An efbundle defaults to the Production environment like a normal ASP.NET Core app.
- A bundle can print the SQL it will execute before running it.
- If one migration fails, the bundle rolls back every migration it applied in that run.
- A bundle built on a Windows agent runs on Linux as long as it is self-contained.