Skip to content

Commit 3e87f84

Browse files
committed
Document migrations connection and offline flags (#5265)
See dotnet/efcore#24271 Closes #5261
1 parent b3078cb commit 3e87f84

3 files changed

Lines changed: 55 additions & 13 deletions

File tree

entity-framework/core/cli/dotnet.md

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -130,10 +130,11 @@ Deletes the database.
130130

131131
Options:
132132

133-
| Option | Short | Description |
134-
|:-------------------------|:------------------|:---------------------------------------------------------|
135-
| <nobr>`--force`</nobr> | <nobr>`-f`</nobr> | Don't confirm. |
136-
| <nobr>`--dry-run`</nobr> | | Show which database would be dropped, but don't drop it. |
133+
| Option | Short | Description |
134+
|:------------------------------------------|:------------------|:--------------------------------------------------------------------------------------------------------------------------------|
135+
| <nobr>`--force`</nobr> | <nobr>`-f`</nobr> | Don't confirm. |
136+
| <nobr>`--dry-run`</nobr> | | Show which database would be dropped, but don't drop it. |
137+
| <nobr>`--connection <CONNECTION>`</nobr> | | The connection string to the database. Defaults to the one specified in `AddDbContext` or `OnConfiguring`. Added in EF Core 11. |
137138

138139
The [common options](#common-options) are listed above.
139140

@@ -341,12 +342,17 @@ Removes the last migration, rolling back the code changes that were done for the
341342

342343
Options:
343344

344-
| Option | Short | Description |
345-
|:-----------------------|:------------------|:--------------------------------------------------------------------------------|
346-
| <nobr>`--force`</nobr> | <nobr>`-f`</nobr> | 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. |
345+
| Option | Short | Description |
346+
|:-----------------------------------------|:------------------|:-----------------------------------------------------------------------------------------------------|
347+
| <nobr>`--force`</nobr> | <nobr>`-f`</nobr> | 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. |
348+
| <nobr>`--connection <CONNECTION>`</nobr> | | The connection string to the database. Defaults to the one specified in `AddDbContext` or `OnConfiguring`. Added in EF Core 11. |
349+
| <nobr>`--offline`</nobr> | | Remove the migration without connecting to the database. Added in EF Core 11. |
347350

348351
The [common options](#common-options) are listed above.
349352

353+
> [!NOTE]
354+
> 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.
355+
350356
## `dotnet ef migrations script`
351357

352358
Generates a SQL script from migrations.

entity-framework/core/cli/powershell.md

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -149,9 +149,10 @@ Drops the database.
149149

150150
Parameters:
151151

152-
| Parameter | Description |
153-
|:-----------------------|:---------------------------------------------------------|
154-
| <nobr>`-WhatIf`</nobr> | Show which database would be dropped, but don't drop it. |
152+
| Parameter | Description |
153+
|:------------------------------------|:--------------------------------------------------------------------------------------------------------------------------------|
154+
| <nobr>`-WhatIf`</nobr> | Show which database would be dropped, but don't drop it. |
155+
| <nobr>`-Connection <String>`</nobr> | The connection string to the database. Defaults to the one specified in `AddDbContext` or `OnConfiguring`. Added in EF Core 11. |
155156

156157
The [common parameters](#common-parameters) are listed above.
157158

@@ -210,12 +211,17 @@ Removes the last migration (rolls back the code changes that were done for the m
210211

211212
Parameters:
212213

213-
| Parameter | Description |
214-
|:----------------------|:--------------------------------------------------------------------------------|
215-
| <nobr>`-Force`</nobr> | Revert the migration (roll back the changes that were applied to the database). |
214+
| Parameter | Description |
215+
|:------------------------------------|:-------------------------------------------------------------------------------------------------------------|
216+
| <nobr>`-Force`</nobr> | Revert the migration (roll back the changes that were applied to the database). |
217+
| <nobr>`-Connection <String>`</nobr> | The connection string to the database. Defaults to the one specified in `AddDbContext` or `OnConfiguring`. Added in EF Core 11. |
218+
| <nobr>`-Offline`</nobr> | Remove the migration without connecting to the database. Added in EF Core 11. |
216219

217220
The [common parameters](#common-parameters) are listed above.
218221

222+
> [!NOTE]
223+
> 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.
224+
219225
## Scaffold-DbContext
220226

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

entity-framework/core/what-is-new/ef-core-11.0/whatsnew.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,36 @@ In PowerShell, use the `-Add` parameter:
195195
Update-Database -Migration InitialCreate -Add
196196
```
197197

198+
<a name="migrations-remove-connection-offline"></a>
199+
200+
### Connection and offline options for migrations remove
201+
202+
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:
203+
204+
```console
205+
# Remove migration with specific connection
206+
dotnet ef migrations remove --connection "Server=prod;Database=MyDb;..."
207+
208+
# Remove migration without connecting to database (offline mode)
209+
dotnet ef migrations remove --offline
210+
211+
# Revert and remove applied migration
212+
dotnet ef migrations remove --force
213+
214+
# Drop specific database by connection string
215+
dotnet ef database drop --connection "Server=test;Database=MyDb;..." --force
216+
```
217+
218+
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.
219+
220+
In PowerShell, use the `-Connection` and `-Offline` parameters:
221+
222+
```powershell
223+
Remove-Migration -Connection "Server=prod;Database=MyDb;..."
224+
Remove-Migration -Offline
225+
Drop-Database -Connection "Server=test;Database=MyDb;..." -Force
226+
```
227+
198228
## SQL Server
199229

200230
<a name="sqlserver-vector-search"></a>

0 commit comments

Comments
 (0)