From 4b146c8ded1174a95c396c0b324a75e2aff822b1 Mon Sep 17 00:00:00 2001 From: Shay Rojansky Date: Tue, 10 Feb 2026 20:07:43 +0100 Subject: [PATCH] Document migrations connection and offline flags See https://github.com/dotnet/efcore/issues/24271 Closes #5261 --- entity-framework/core/cli/dotnet.md | 20 ++++++++----- entity-framework/core/cli/powershell.md | 18 +++++++---- .../core/what-is-new/ef-core-11.0/whatsnew.md | 30 +++++++++++++++++++ 3 files changed, 55 insertions(+), 13 deletions(-) diff --git a/entity-framework/core/cli/dotnet.md b/entity-framework/core/cli/dotnet.md index 8aa42a2382..4d28cf8cb0 100644 --- a/entity-framework/core/cli/dotnet.md +++ b/entity-framework/core/cli/dotnet.md @@ -130,10 +130,11 @@ Deletes the database. Options: -| Option | Short | Description | -|:-------------------------|:------------------|:---------------------------------------------------------| -| `--force` | `-f` | Don't confirm. | -| `--dry-run` | | Show which database would be dropped, but don't drop it. | +| Option | Short | Description | +|:------------------------------------------|:------------------|:--------------------------------------------------------------------------------------------------------------------------------| +| `--force` | `-f` | Don't confirm. | +| `--dry-run` | | Show which database would be dropped, but don't drop it. | +| `--connection ` | | The connection string to the database. Defaults to the one specified in `AddDbContext` or `OnConfiguring`. Added in EF Core 11. | The [common options](#common-options) are listed above. @@ -341,12 +342,17 @@ Removes the last migration, rolling back the code changes that were done for the Options: -| Option | Short | Description | -|:-----------------------|:------------------|:--------------------------------------------------------------------------------| -| `--force` | `-f` | Revert the latest migration, rolling back both code and database changes that were done for the latest migration. Continues to roll back only the code changes if an error occurs while connecting to the database. | +| Option | Short | Description | +|:-----------------------------------------|:------------------|:-----------------------------------------------------------------------------------------------------| +| `--force` | `-f` | Revert the latest migration, rolling back both code and database changes that were done for the latest migration. Continues to roll back only the code changes if an error occurs while connecting to the database. | +| `--connection ` | | The connection string to the database. Defaults to the one specified in `AddDbContext` or `OnConfiguring`. Added in EF Core 11. | +| `--offline` | | Remove the migration without connecting to the database. Added in EF Core 11. | The [common options](#common-options) are listed above. +> [!NOTE] +> The `--offline` and `--force` options cannot be used together, since `--force` requires a database connection to check if the migration has been applied before reverting it. + ## `dotnet ef migrations script` Generates a SQL script from migrations. diff --git a/entity-framework/core/cli/powershell.md b/entity-framework/core/cli/powershell.md index 3b04f42073..8868bcd962 100644 --- a/entity-framework/core/cli/powershell.md +++ b/entity-framework/core/cli/powershell.md @@ -149,9 +149,10 @@ Drops the database. Parameters: -| Parameter | Description | -|:-----------------------|:---------------------------------------------------------| -| `-WhatIf` | Show which database would be dropped, but don't drop it. | +| Parameter | Description | +|:------------------------------------|:--------------------------------------------------------------------------------------------------------------------------------| +| `-WhatIf` | Show which database would be dropped, but don't drop it. | +| `-Connection ` | The connection string to the database. Defaults to the one specified in `AddDbContext` or `OnConfiguring`. Added in EF Core 11. | The [common parameters](#common-parameters) are listed above. @@ -210,12 +211,17 @@ Removes the last migration (rolls back the code changes that were done for the m Parameters: -| Parameter | Description | -|:----------------------|:--------------------------------------------------------------------------------| -| `-Force` | Revert the migration (roll back the changes that were applied to the database). | +| Parameter | Description | +|:------------------------------------|:-------------------------------------------------------------------------------------------------------------| +| `-Force` | Revert the migration (roll back the changes that were applied to the database). | +| `-Connection ` | The connection string to the database. Defaults to the one specified in `AddDbContext` or `OnConfiguring`. Added in EF Core 11. | +| `-Offline` | Remove the migration without connecting to the database. Added in EF Core 11. | The [common parameters](#common-parameters) are listed above. +> [!NOTE] +> The `-Offline` and `-Force` parameters cannot be used together, since `-Force` requires a database connection to check if the migration has been applied before reverting it. + ## Scaffold-DbContext Generates code for a `DbContext` and entity types for a database. In order for `Scaffold-DbContext` to generate an entity type, the database table must have a primary key. diff --git a/entity-framework/core/what-is-new/ef-core-11.0/whatsnew.md b/entity-framework/core/what-is-new/ef-core-11.0/whatsnew.md index b5969366d1..6519a37ce9 100644 --- a/entity-framework/core/what-is-new/ef-core-11.0/whatsnew.md +++ b/entity-framework/core/what-is-new/ef-core-11.0/whatsnew.md @@ -195,6 +195,36 @@ In PowerShell, use the `-Add` parameter: Update-Database -Migration InitialCreate -Add ``` + + +### Connection and offline options for migrations remove + +The `dotnet ef migrations remove` and `database drop` commands now accept `--connection` parameters, allowing you to specify the database connection string directly without needing to configure a default connection in your `DbContext`. Additionally, `migrations remove` supports the new `--offline` option to remove a migration without connecting to the database: + +```console +# Remove migration with specific connection +dotnet ef migrations remove --connection "Server=prod;Database=MyDb;..." + +# Remove migration without connecting to database (offline mode) +dotnet ef migrations remove --offline + +# Revert and remove applied migration +dotnet ef migrations remove --force + +# Drop specific database by connection string +dotnet ef database drop --connection "Server=test;Database=MyDb;..." --force +``` + +The `--offline` option skips the database connection check entirely, which is useful when the database is inaccessible or when you're certain the migration hasn't been applied. Note that `--offline` and `--force` cannot be used together, since `--force` requires a database connection to check if the migration has been applied before reverting it. + +In PowerShell, use the `-Connection` and `-Offline` parameters: + +```powershell +Remove-Migration -Connection "Server=prod;Database=MyDb;..." +Remove-Migration -Offline +Drop-Database -Connection "Server=test;Database=MyDb;..." -Force +``` + ## SQL Server