Skip to content

Add --connection and --offline parameters to migrations remove and database drop commands#37657

Merged
AndriySvyryd merged 9 commits intomainfrom
copilot/add-connection-parameter-migrations
Feb 10, 2026
Merged

Add --connection and --offline parameters to migrations remove and database drop commands#37657
AndriySvyryd merged 9 commits intomainfrom
copilot/add-connection-parameter-migrations

Conversation

Copy link
Contributor

Copilot AI commented Feb 9, 2026

Fixes #24271

EF Core CLI commands migrations remove and database drop now 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 removal
  • dotnet ef migrations remove --offline - remove migration without connecting to the database
  • dotnet ef migrations remove --force - revert the migration if it has been applied to the database
  • dotnet ef database drop --connection <CONNECTION> - specify connection string for database drop

PMC Commands:

  • Remove-Migration -Connection <CONNECTION> - specify connection string for migration removal
  • Remove-Migration -Offline - remove migration without connecting to the database
  • Remove-Migration -Force - revert the migration if it has been applied to the database
  • Drop-Database -Connection <CONNECTION> - specify connection string for database drop

Implementation:

  • Extended MigrationsRemoveCommand and DatabaseDropCommand to accept connection parameters
  • Updated IOperationExecutor interface and implementations to propagate connection strings and offline flag
  • Modified MigrationsOperations.RemoveMigration() and DbContextOperations.DropDatabase() to apply connection strings via context.Database.SetConnectionString()
  • Updated IMigrationsScaffolder and MigrationsScaffolder to handle offline mode by skipping database connection entirely
  • Added corresponding PMC command support in EntityFrameworkCore.psm1

Usage

# 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

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.
  • Validation: The --offline and --force options 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.

Copilot AI and others added 2 commits February 9, 2026 19:29
…ommands

Co-authored-by: AndriySvyryd <6539701+AndriySvyryd@users.noreply.github.com>
Co-authored-by: AndriySvyryd <6539701+AndriySvyryd@users.noreply.github.com>
Copilot AI changed the title [WIP] Add connection parameter for Remove-Migration and Drop-Database Add --connection and --offline parameters to migrations remove and database drop commands Feb 9, 2026
Copilot AI requested a review from AndriySvyryd February 9, 2026 19:39
Copy link
Member

@AndriySvyryd AndriySvyryd left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@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>
Copy link
Contributor Author

Copilot AI commented Feb 9, 2026

@copilot You need to update the PMC commands in EntityFrameworkCore.psm1

Fixed in 8dfe8d0. Updated Remove-Migration to support -Connection and -Offline parameters, and Drop-Database to support -Connection parameter.

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>
Copilot AI requested a review from AndriySvyryd February 9, 2026 20:48
@AndriySvyryd AndriySvyryd marked this pull request as ready for review February 9, 2026 20:54
@AndriySvyryd AndriySvyryd requested a review from a team as a code owner February 9, 2026 20:54
Copilot AI review requested due to automatic review settings February 9, 2026 20:54
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 --connection to dotnet ef migrations remove and dotnet ef database drop, plus --offline to dotnet ef migrations remove.
  • Propagated connectionString/offline through the operation executor pipeline and applied it in MigrationsOperations/DbContextOperations.
  • Added offline support in MigrationsScaffolder.RemoveMigration by 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

@AndriySvyryd AndriySvyryd removed the request for review from a team February 9, 2026 21:33
- 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>
@AndriySvyryd
Copy link
Member

@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>
Copy link
Contributor Author

Copilot AI commented Feb 10, 2026

@copilot Revert the PR description to say what the PR does overall, not what was done in the latest commits

Updated PR description to provide a high-level overview of the feature rather than a checklist of incremental changes.

@AndriySvyryd AndriySvyryd merged commit 2eb51f7 into main Feb 10, 2026
13 checks passed
@AndriySvyryd AndriySvyryd deleted the copilot/add-connection-parameter-migrations branch February 10, 2026 17:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add --connection parameter for Remove-Migration

4 participants