Add --connection and --offline parameters to migrations remove and database drop commands#37657
Conversation
…ommands Co-authored-by: AndriySvyryd <6539701+AndriySvyryd@users.noreply.github.com>
Co-authored-by: AndriySvyryd <6539701+AndriySvyryd@users.noreply.github.com>
AndriySvyryd
left a comment
There was a problem hiding this comment.
@copilot You need to update the PMC commands in EntityFrameworkCore.psm1
- Replace DatabaseDropConnectionDescription with DbContextConnectionDescription - Add separate offline parameter to RemoveMigration that skips database connection - Update IOperationExecutor, OperationExecutor, MigrationsOperations, and MigrationsScaffolder - Update PMC commands to support --connection and --offline parameters - Force behavior: connects to DB, reverts migration if applied - Offline behavior: skips DB connection entirely Co-authored-by: AndriySvyryd <6539701+AndriySvyryd@users.noreply.github.com>
Instead of marking the overload as obsolete, moved the offline parameter to the end and made it optional with a default value of false. This maintains backward compatibility without deprecation warnings. Co-authored-by: AndriySvyryd <6539701+AndriySvyryd@users.noreply.github.com>
There was a problem hiding this comment.
Pull request overview
This PR extends EF Core tooling (CLI + PMC) so migrations remove and database drop can accept an explicit connection string, and so migrations remove can run in an offline mode that skips database connectivity checks.
Changes:
- Added
--connectiontodotnet ef migrations removeanddotnet ef database drop, plus--offlinetodotnet ef migrations remove. - Propagated
connectionString/offlinethrough the operation executor pipeline and applied it inMigrationsOperations/DbContextOperations. - Added offline support in
MigrationsScaffolder.RemoveMigrationby skipping the applied-migration check when requested, and updated PMC cmdlets to pass the new flags.
Reviewed changes
Copilot reviewed 14 out of 16 changed files in this pull request and generated 8 comments.
Show a summary per file
| File | Description |
|---|---|
| src/ef/Properties/Resources.resx | Adds localized description for --offline option (ef command resources). |
| src/ef/Properties/Resources.Designer.cs | Adds strongly-typed resource accessor for the new offline description. |
| src/ef/OperationExecutorBase.cs | Extends operation invocation args for remove-migration and drop-database to include offline/connection string. |
| src/ef/IOperationExecutor.cs | Updates executor interface to accept offline/connection string for remove-migration and connection string for drop-database. |
| src/ef/Commands/MigrationsRemoveCommand.cs | Passes --offline/--connection through to the executor. |
| src/ef/Commands/MigrationsRemoveCommand.Configure.cs | Defines --offline and --connection options for migrations remove. |
| src/ef/Commands/DatabaseDropCommand.cs | Passes --connection through to the executor for database drop. |
| src/ef/Commands/DatabaseDropCommand.Configure.cs | Defines --connection option for database drop. |
| src/dotnet-ef/Properties/Resources.resx | Adds localized description for --offline option (dotnet-ef resources). |
| src/dotnet-ef/Properties/Resources.Designer.cs | Adds strongly-typed resource accessor for the new offline description. |
| src/EFCore.Tools/tools/EntityFrameworkCore.psm1 | Adds -Connection for Drop-Database and -Offline/-Connection for Remove-Migration PMC cmdlets. |
| src/EFCore.Design/Migrations/Design/MigrationsScaffolder.cs | Implements offline removal by skipping the applied-migration DB query when requested. |
| src/EFCore.Design/Migrations/Design/IMigrationsScaffolder.cs | Adds an offline parameter to the public scaffolder interface remove method. |
| src/EFCore.Design/Design/OperationExecutor.cs | Plumbs offline and connectionString args into the design-time operations for remove-migration and drop-database. |
| src/EFCore.Design/Design/Internal/MigrationsOperations.cs | Applies SetConnectionString(...) and passes offline through to the scaffolder. |
| src/EFCore.Design/Design/Internal/DbContextOperations.cs | Applies SetConnectionString(...) before dropping the database. |
Files not reviewed (2)
- src/dotnet-ef/Properties/Resources.Designer.cs: Language not supported
- src/ef/Properties/Resources.Designer.cs: Language not supported
- Add validation to prevent using --offline and --force together - Reformat dictionary initializers to multiple lines for readability - Fix XML documentation for force parameter to reflect actual behavior - Remove trailing whitespace in EntityFrameworkCore.psm1 - Handle --connection parameter in DatabaseDropCommand prompt/dry-run Co-authored-by: AndriySvyryd <6539701+AndriySvyryd@users.noreply.github.com>
- Add connectionString parameter to GetContextInfo interface and implementation - Revert custom logic in DatabaseDropCommand to use GetContextInfo with connection - Move --offline and --force validation from Execute to Validate method - Add OfflineForceConflict resource string - Update all callers of GetContextInfo to pass null for connection when not needed Co-authored-by: AndriySvyryd <6539701+AndriySvyryd@users.noreply.github.com>
- Add default value of null to connectionString parameter in IOperationExecutor.GetContextInfo - Update implementation in OperationExecutorBase - Simplify callers by removing explicit null arguments Co-authored-by: AndriySvyryd <6539701+AndriySvyryd@users.noreply.github.com>
|
@copilot Revert the PR description to say what the PR does overall, not what was done in the latest commits |
…ntextInfo Add default value of null to connectionString parameter to match the interface signature and simplify callers. Co-authored-by: AndriySvyryd <6539701+AndriySvyryd@users.noreply.github.com>
Updated PR description to provide a high-level overview of the feature rather than a checklist of incremental changes. |
Fixes #24271
EF Core CLI commands
migrations removeanddatabase dropnow accept connection strings as parameters, eliminating the need to configure default connections in DbContext for these operations.Changes
New Parameters:
dotnet ef migrations remove --connection <CONNECTION>- specify connection string for migration removaldotnet ef migrations remove --offline- remove migration without connecting to the databasedotnet ef migrations remove --force- revert the migration if it has been applied to the databasedotnet ef database drop --connection <CONNECTION>- specify connection string for database dropPMC Commands:
Remove-Migration -Connection <CONNECTION>- specify connection string for migration removalRemove-Migration -Offline- remove migration without connecting to the databaseRemove-Migration -Force- revert the migration if it has been applied to the databaseDrop-Database -Connection <CONNECTION>- specify connection string for database dropImplementation:
MigrationsRemoveCommandandDatabaseDropCommandto accept connection parametersIOperationExecutorinterface and implementations to propagate connection strings and offline flagMigrationsOperations.RemoveMigration()andDbContextOperations.DropDatabase()to apply connection strings viacontext.Database.SetConnectionString()IMigrationsScaffolderandMigrationsScaffolderto handle offline mode by skipping database connection entirelyEntityFrameworkCore.psm1Usage
Behavior
--offline: Skips the database connection check entirely. Useful when the database is inaccessible or when you're certain the migration hasn't been applied.--force: Connects to the database to check if the migration has been applied. If it has, reverts the migration before removing the files.--offlineand--forceoptions cannot be used together, as offline mode prevents the database check required for force revert.✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.