Entity Framework Core migrations help developers keep the database schema synchronized with changes in .NET application models. During development, applying migrations is straightforward. Developers can run dotnet ef database update, and EF Core updates the database to match the latest migration.
However, production deployments require a more controlled approach.
Running migration commands directly on production servers can introduce deployment dependencies, security risks, and operational problems. Installing the .NET SDK on production machines is unnecessary, and allowing every application instance to modify the database schema can cause conflicts.
EF Core provides migration bundles to address these challenges. A migration bundle is an executable deployment artifact that contains the migrations required to update a database. It can be generated during the build process and executed separately during deployment.
This approach allows database schema updates to run independently of the main application deployment. It also avoids requiring the .NET SDK, EF Core command-line tools, or application source code on the deployment machine.
This article explains how migration bundles work, how to generate and execute them, how to configure them for different environments, and how to integrate them into a production deployment pipeline.
What Are EF Core Migration Bundles?
An EF Core migration bundle is a standalone executable generated from an application's existing Entity Framework Core migrations.
The executable contains the migration code needed to update a database. When executed, it connects to the configured database, checks which migrations have already been applied, and executes the pending migrations.
For example, suppose an application has the following migrations:
Migrations/
20260110090000_InitialCreate.cs
20260215110000_AddCustomerTable.cs
20260320120000_AddOrderStatus.cs
AppDbContextModelSnapshot.csInstead of executing these migrations directly from the application project, developers can generate a bundle:
dotnet ef migrations bundleEF Core creates an executable named efbundle by default. On Windows, the executable normally has the .exe extension.
The deployment system can execute this file to apply the pending migrations.
A migration bundle provides several advantages:
It does not require the application's source code on the production server.
It does not require the .NET SDK or EF Core CLI tools at execution time.
It can include the .NET runtime when generated as a self-contained bundle.
It supports controlled database deployment.
It uses EF Core migration locking to help coordinate migration execution.
It can run configured EF Core seeding logic.
A bundle is particularly useful when database changes need to be deployed through a CI/CD pipeline.
Why Use Migration Bundles Instead of Runtime Migrations?
A common approach in small applications is to execute migrations when the application starts.
For example:
using (var scope = app.Services.CreateScope())
{
var dbContext = scope.ServiceProvider
.GetRequiredService<AppDbContext>();
await dbContext.Database.MigrateAsync();
}This approach can work in development environments. However, it introduces operational risks in production.
Imagine an ASP.NET Core application running four instances behind a load balancer. If all four instances start simultaneously, each may attempt to perform database migration work during startup.
Even though newer EF Core versions include migration locking, application startup is still not an ideal place for managing production schema changes. Database permissions, deployment ordering, application availability, and schema compatibility become harder to control.
Migration bundles separate database deployment from application startup.
Approach | Description | Recommended use |
|---|---|---|
Runtime migrations | Application applies migrations during startup | Development or carefully controlled scenarios |
| EF CLI applies migrations | Local development and controlled environments |
SQL migration scripts | Generated SQL is executed separately | DBA-reviewed production deployments |
Migration bundles | Standalone executable applies migrations | Automated deployment pipelines |
Microsoft recommends using migration bundles for automated deployment scenarios and SQL scripts when generated SQL must be reviewed, modified, or approved before execution.
Prerequisites for Creating Migration Bundles
Before creating a migration bundle, make sure the project has the required EF Core packages.
For this example, we will use an ASP.NET Core application with EF Core 10 and SQL Server.
Install the SQL Server provider:
dotnet add package Microsoft.EntityFrameworkCore.SqlServer --version 10.0.0Install the EF Core design package:
dotnet add package Microsoft.EntityFrameworkCore.Design --version 10.0.0Install the EF Core command-line tool:
dotnet tool install --global dotnet-ef --version 10.0.0If the tool is already installed, update it:
dotnet tool update --global dotnet-ef --version 10.0.0Use compatible versions of the EF Core runtime, database provider, design package, and tools. Keeping these versions aligned helps avoid migration-generation and runtime compatibility problems.
Verify the installed tool:
dotnet ef --versionThe command should display the installed EF Core tools version.
Create an ASP.NET Core Project With EF Core
Let's create a simple application that manages customers and orders.
Create a Web API project:
dotnet new webapi -n MigrationBundleDemo
cd MigrationBundleDemoCreate a Models folder and add a Customer.cs file:
namespace MigrationBundleDemo.Models;
public class Customer
{
public int Id { get; set; }
public string Name { get; set; } = string.Empty;
public string Email { get; set; } = string.Empty;
public DateTime CreatedAt { get; set; }
}Create an AppDbContext.cs file:
using Microsoft.EntityFrameworkCore;
using MigrationBundleDemo.Models;
namespace MigrationBundleDemo.Data;
public class AppDbContext : DbContext
{
public AppDbContext(
DbContextOptions<AppDbContext> options)
: base(options)
{
}
public DbSet<Customer> Customers =>
Set<Customer>();
}Configure the database connection in appsettings.json:
{
"ConnectionStrings": {
"DefaultConnection": "Server=localhost;Database=MigrationBundleDemo;Trusted_Connection=True;TrustServerCertificate=True"
}
}For this local example, the connection string uses SQL Server Windows authentication. Production credentials should be provided through a secure configuration source rather than committed to source control.
Register the DbContext in Program.cs:
using Microsoft.EntityFrameworkCore;
using MigrationBundleDemo.Data;
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddDbContext<AppDbContext>(
options =>
options.UseSqlServer(
builder.Configuration.GetConnectionString(
"DefaultConnection")));
builder.Services.AddControllers();
var app = builder.Build();
app.MapControllers();
app.Run();The application now has the database configuration required for EF Core migrations.
Create the First EF Core Migration
Before generating a bundle, the project must contain migrations.
Run the following command:
dotnet ef migrations add InitialCreateEF Core compares the current model with the model snapshot and generates the migration files.
The project structure will look similar to this:
MigrationBundleDemo/
│
├── Data/
│ └── AppDbContext.cs
│
├── Models/
│ └── Customer.cs
│
├── Migrations/
│ ├── 20261201090000_InitialCreate.cs
│ ├── 20261201090000_InitialCreate.Designer.cs
│ └── AppDbContextModelSnapshot.cs
│
├── Program.cs
├── appsettings.json
└── MigrationBundleDemo.csprojThe generated migration contains the database operations required to create the customer table.
For example, its Up() method may contain:
migrationBuilder.CreateTable(
name: "Customers",
columns: table => new
{
Id = table.Column<int>(
type: "int",
nullable: false)
.Annotation(
"SqlServer:Identity",
"1, 1"),
Name = table.Column<string>(
type: "nvarchar(max)",
nullable: false),
Email = table.Column<string>(
type: "nvarchar(max)",
nullable: false),
CreatedAt = table.Column<DateTime>(
type: "datetime2",
nullable: false)
},
constraints: table =>
{
table.PrimaryKey(
"PK_Customers",
x => x.Id);
});The actual generated code may differ depending on the model configuration and provider version.
Review generated migrations before deploying them. For example, an unintended column deletion can result in data loss.
Generate an EF Core Migration Bundle
Once the migration exists, generate the bundle using the following command:
dotnet ef migrations bundleEF Core builds the project and creates the executable.
The default output is:
efbundle.exeon Windows, or:
efbundleon Linux and macOS.
You can specify the output location:
dotnet ef migrations bundle \
--output artifacts/efbundleOn Windows PowerShell:
dotnet ef migrations bundle `
--output artifacts\efbundle.exeThe output folder now contains the executable that can be used during deployment.
Generate a self-contained migration bundle
A normal bundle may require a compatible .NET runtime on the machine where it runs.
If the deployment environment does not have the required runtime, generate a self-contained bundle.
For example, to create a Linux x64 bundle:
dotnet ef migrations bundle \
--self-contained \
--target-runtime linux-x64 \
--output artifacts/efbundleFor Windows x64:
dotnet ef migrations bundle \
--self-contained \
--target-runtime win-x64 \
--output artifacts/efbundle.exeFor Linux ARM64:
dotnet ef migrations bundle \
--self-contained \
--target-runtime linux-arm64 \
--output artifacts/efbundleA self-contained bundle includes the .NET runtime required by the application. It does not require the target machine to have that runtime installed.
However, the bundle is still platform-specific. Generate it for the operating system and architecture where it will execute.
Overwrite an existing bundle
If the output file already exists, use the --force option:
dotnet ef migrations bundle \
--force \
--output artifacts/efbundleThis is useful in CI/CD pipelines where the bundle is regenerated for each release.
Execute the Migration Bundle
After generating the bundle, execute it against the target database.
On Windows:
.\efbundle.exeOn Linux:
chmod +x ./efbundle
./efbundleThe executable connects to the configured database and applies pending migrations.
The bundle uses the EF Core migrations history table, normally named __EFMigrationsHistory, to determine which migrations have already been applied.
If the database is already up to date, executing the same bundle again does not reapply completed migrations.
For example, the output may look like this:
Applying migration '20261201090000_InitialCreate'.
Done.If the database already contains that migration:
No migrations were applied.
The database is already up to date.This behavior makes migration bundles suitable for repeatable deployment pipelines.
Provide the database connection string
In production environments, the database connection should come from a secure deployment configuration.
You can pass it directly through the --connection option:
./efbundle \
--connection "$DEPLOYMENT_CONNECTION_STRING"On Windows PowerShell:
.\efbundle.exe `
--connection $env:DEPLOYMENT_CONNECTION_STRINGThe deployment system should retrieve the connection string from a secret store or protected pipeline variable.
Join the conversation! Your thoughts help the community grow.