Skip to content

Conversation

@thromel
Copy link
Contributor

@thromel thromel commented Dec 23, 2025

Summary

Implements #37342: Allow creating and applying migrations at runtime without recompiling.

This adds support for creating and applying migrations at runtime using Roslyn compilation, enabling scenarios like .NET Aspire and containerized applications where recompilation isn't possible.

CLI Usage

# Standard update (existing behavior)
dotnet ef database update [migration]

# Create and apply a new migration in one step
dotnet ef database update MigrationName --add [--output-dir <DIR>] [--namespace <NS>] [--json]

The -o/--output-dir, -n/--namespace, and --json options require --add to be specified.

PowerShell Usage

# Standard update (existing behavior)
Update-Database [-Migration <migration>]

# Create and apply a new migration in one step
Update-Database -Migration MigrationName -Add [-OutputDir <DIR>] [-Namespace <NS>]

Architecture

Component Purpose
IMigrationCompiler / CSharpMigrationCompiler Internal: Roslyn-based compilation of scaffolded migrations
IMigrationsAssembly.AddMigrations(Assembly) Registers dynamically compiled migrations
MigrationsOperations.AddAndApplyMigration() Orchestrates scaffold → compile → register → apply workflow

Design Decisions

  • Extends existing services: Uses IMigrationsScaffolder for scaffolding and IMigrator for applying, adding only the new IMigrationCompiler service
  • AddMigrations(Assembly): Extended IMigrationsAssembly interface to accept additional assemblies containing runtime-compiled migrations
  • Always persists to disk: Like AddMigration, files are always saved to enable source control and future recompilation
  • No pending changes behavior: If no model changes are detected, applies any existing pending migrations without creating a new one
  • Internal compiler API: IMigrationCompiler and CSharpMigrationCompiler are in the .Internal namespace as they require design work for public API
  • Error handling with cleanup: If compilation or migration application fails, saved migration files are cleaned up to prevent orphans
  • Thread safety: MigrationsAssembly uses locking to protect against race conditions when adding migrations concurrently

Workflow

User runs: dotnet ef database update InitialCreate --add
    │
    ▼
MigrationsOperations.AddAndApplyMigration()
    │
    ├─► Check for pending model changes
    │       └─► If none: apply existing migrations, return
    │
    ├─► IMigrationsScaffolder.ScaffoldMigration() - Generate code
    │
    ├─► try {
    │       ├─► IMigrationsScaffolder.Save() - Write files to disk
    │       ├─► IMigrationCompiler.CompileMigration() - Roslyn compile
    │       ├─► IMigrationsAssembly.AddMigrations() - Register migration
    │       └─► IMigrator.Migrate() - Apply to database
    │   } catch {
    │       └─► Clean up saved files on failure
    │   }
    │
    └─► Return migration files

Robustness Features

  1. Exception handling with cleanup: AddAndApplyMigration wraps the save-compile-register-apply chain in try-catch, deleting saved files on failure to prevent orphans
  2. Context disposal on validation failure: PrepareForMigration ensures the DbContext is disposed if validation or service building fails
  3. Thread-safe migration registration: MigrationsAssembly uses locking to protect shared state (migrations dictionary, model snapshot, additional assemblies list)

Limitations

  • Requires dynamic code generation (incompatible with NativeAOT) - marked with [RequiresDynamicCode]
  • C# only (no VB.NET/F# support)

Test plan

  • Unit tests for CSharpMigrationCompiler
  • Unit tests for MigrationsOperations.AddAndApplyMigration
  • Integration tests in RuntimeMigrationTestBase (SQLite and SQL Server implementations)
  • Tests for validation (empty name, invalid characters)
  • Tests for RemoveMigration with dynamically created migrations
  • All existing EFCore.Design.Tests pass
  • All existing EFCore.Relational.Tests pass

Fixes #37342

@thromel thromel force-pushed the feature/runtime-migrations branch from a15611a to 9a35a9b Compare December 23, 2025 20:45
@AndriySvyryd AndriySvyryd self-assigned this Dec 23, 2025
@thromel thromel marked this pull request as ready for review December 24, 2025 06:22
@thromel thromel requested a review from a team as a code owner December 24, 2025 06:22
@thromel thromel marked this pull request as draft December 25, 2025 02:03
@thromel thromel force-pushed the feature/runtime-migrations branch from 62018f1 to 5c41f2f Compare December 25, 2025 03:51
@thromel
Copy link
Contributor Author

thromel commented Dec 25, 2025

Note on SQL Server Integration Tests

The RuntimeMigrationSqlServerTest tests are marked with [SqlServerCondition(SqlServerCondition.IsNotAzureSql | SqlServerCondition.IsNotCI)] and are skipped in CI. This follows the same pattern used by MigrationsInfrastructureSqlServerTest.

Why these tests are skipped in CI:

  • They require creating fresh databases dynamically for each test to properly test the migration flow from scratch
  • The Helix CI environment has SQL Server available but with limited permissions configured for shared/pre-configured databases
  • Tests that use SqlServerTestStore.CreateInitializedAsync with dynamic database names don't work in the CI SQL Server setup

Test coverage is still maintained:

  • Core runtime migration logic is covered by unit tests in EFCore.Design.Tests (which run in CI)
  • The SQL Server integration tests run locally for developers with SQL Server configured
  • SQLite integration tests in EFCore.Sqlite.FunctionalTests also validate the end-to-end flow

This is consistent with how other complex migration infrastructure tests handle CI limitations.

@thromel thromel marked this pull request as ready for review December 25, 2025 08:13
@thromel thromel marked this pull request as draft December 25, 2025 16:43
@thromel thromel marked this pull request as ready for review December 25, 2025 21:07
@thromel
Copy link
Contributor Author

thromel commented Dec 31, 2025

Thank you for the thorough review @AndriySvyryd! I've addressed all your feedback:

  1. SQL Server tests: Now using static database name "RuntimeMigrationTest" and removed EnsureDeleted() calls since CreateInitializedAsync already cleans the database.

  2. CLI validation: Added validation that shows an error if -o or -n is used without --add.

  3. Renamed to AddAndApplyMigration: Renamed CreateAndApplyMigration to AddAndApplyMigration and reordered parameters as suggested.

  4. Extracted common validation: Created ValidateMigrationName() and ValidateMigrationNameNotContextName() helper methods that are shared by both AddMigration and AddAndApplyMigration.

  5. Added EnsureMigrationsAssembly call: Now calling EnsureMigrationsAssembly in AddAndApplyMigration.

  6. Removed unnecessary IDesignTimeModel registration: Confirmed there's no duplicate registration.

  7. Merged IDynamicMigrationsAssembly into IMigrationsAssembly: Added AddMigrations(Assembly) method to IMigrationsAssembly and deleted IDynamicMigrationsAssembly, DynamicMigrationsAssembly, and their tests.

  8. Replaced CompiledMigration with AddMigrations(Assembly): IMigrationCompiler.CompileMigration now returns Assembly directly, and RuntimeMigrationService uses _migrationsAssembly.AddMigrations() to register compiled migrations. Removed the CompiledMigration class.

All tests pass locally (SQLite: 26 tests, SQL Server: 7 tests, CSharpMigrationCompiler: 4 tests, MigrationsOperations: 2 tests).

@thromel thromel requested a review from AndriySvyryd December 31, 2025 05:21
@thromel

This comment was marked as outdated.

- Extract PrepareForMigration helper to consolidate common validation
  between AddMigration and AddAndApplyMigration methods
- Simplify CSharpMigrationCompiler by removing arbitrary assembly prefix
  filtering - now includes all non-dynamic assemblies as reviewer suggested
- Add RemoveMigration_removes_dynamically_created_migration test that
  exercises the full lifecycle: scaffold -> save -> compile -> register
  -> apply -> revert -> remove
Per PR review comment 2663355462: Move the Validate() method from
DatabaseUpdateCommand.cs to DatabaseUpdateCommand.Configure.cs so
the validation is also included in the dotnet-ef tool.

Added required resource strings to dotnet-ef:
- MissingArgument
- OutputDirRequiresAdd
- NamespaceRequiresAdd
Override CleanDatabase in RuntimeMigrationSqlServerTest to properly
handle foreign key constraints. SQL Server requires dropping FK
constraints before dropping tables, unlike SQLite which handles this
automatically with DROP TABLE IF EXISTS.

The fix:
1. Drops all foreign key constraints first using dynamic SQL
2. Then drops all tables
3. Finally drops the migrations history table
Properly restore connection state after cleaning database tables.
The connection is closed after cleanup only if it wasn't already
open before, preventing "connection was not closed" errors in tests
that expect to open the connection themselves.
1. AddAndApplyMigration error handling:
   - Add try-catch around scaffold-compile-apply chain
   - Clean up saved files on failure to prevent orphans
   - Add TryDeleteFile helper for best-effort cleanup
   - Add AddAndApplyMigrationFailed resource string

2. Context disposal in PrepareForMigration:
   - Wrap context usage in try-catch
   - Dispose context if validation or service building fails
   - Prevents context leaks on validation exceptions

3. Thread safety in MigrationsAssembly:
   - Add lock protection around _additionalAssemblies, _migrations, and _modelSnapshot
   - Protect Migrations property getter, ModelSnapshot property getter, and AddMigrations method
   - Prevents race conditions in multi-threaded scenarios
The snapshot file may have overwritten an existing snapshot during
Save(). Deleting it on failure would leave the project without a
snapshot, breaking future migrations. Only delete migration and
metadata files which are always newly created.
- Remove file deletion on failure (keep files for debugging)
- Inline validation methods into PrepareForMigration
- Remove DisableParallelization from test classes
- Refactor tests to use SharedStoreFixtureBase pattern
- Use NonCapturingLazyInitializer for MigrationsAssembly.Migrations
- Convert to using declarations to reduce nesting
- Make CleanDatabase virtual for provider overrides
- Fix thread safety with lock-based ModelSnapshot caching
@thromel thromel force-pushed the feature/runtime-migrations branch from f3a91b0 to cd324fd Compare January 8, 2026 04:41
@thromel

This comment was marked as off-topic.

- Move migration name validation before context creation in AddMigration and
  AddAndApplyMigration to ensure proper error messages when name is empty
- Use Single() instead of First() in Migration_preserves_existing_data test
  to avoid FirstWithoutOrderByAndFilterWarning
Replace First() with Single() to avoid FirstWithoutOrderByAndFilterWarning
Close connection before migrator.Migrate("0") call and reopen after,
since the migrator manages its own connection state internally.
@AndriySvyryd
Copy link
Member

Practical Risk Assessment

The race is unlikely because:

  • AddMigrations is only called during design-time database update --auto operations
  • It's not expected to be called concurrently with Migrations access
  • The scenario requires very specific timing

I agree that it's very unlikely, keep NonCapturingLazyInitializer and remove the lock from AddMigrations

public void Can_scaffold_migration()
{
using var context = CreateContext();
CleanDatabase(context);
Copy link
Member

Choose a reason for hiding this comment

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

You should be able to call Fixture.ReseedAsync() instead.

You can also call it from IAsyncLifetime.InitializeAsync to avoid repeating the call in each method

Copy link
Member

Choose a reason for hiding this comment

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

Sorry, I forgot that ReseedAsync also creates tables. Change InitializeAsync to

{
    using var context = CreateContext();
    return Fixture.TestStore.CleanAsync(context, createTables: false);
}

Then add the bool createTables = true parameter to TestStore.CleanAsync, SqlServerDatabaseFacadeExtensions.EnsureClean, SqliteDatabaseFacadeTestExtensions.EnsureClean and RelationalDatabaseCleaner.Clean

=> "RuntimeMigration";

protected override bool UsePooling
=> false;
Copy link
Member

Choose a reason for hiding this comment

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

Why did you have to specify false here?

Copy link
Member

@AndriySvyryd AndriySvyryd Jan 9, 2026

Choose a reason for hiding this comment

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

Re: UsePooling => false in RuntimeMigrationFixtureBase

Connection pooling is disabled because these tests dynamically alter the database schema (creating/dropping tables, applying/reverting migrations). With pooling enabled, pooled connections might hold stale schema information or cached state that conflicts with the schema changes made during migration operations.
However, if you think pooling should work fine for these tests, I can remove the override and test it.

This property controls DbContext pooling, not connection pooling, so leaving the default value (true) should be fine for these tests

Copy link
Member

Choose a reason for hiding this comment

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

I see now, if you use context pooling then the tests will get a context instance that still contains the runtime migrations assembly from the previous test, making it fail in most cases. So in this case, disabling DbContext pooling is the right choice

Per review: race condition is very unlikely since AddMigrations is only
called during design-time operations, not concurrently with Migrations access.
@thromel
Copy link
Contributor Author

thromel commented Jan 9, 2026

Re: UsePooling => false in RuntimeMigrationFixtureBase

Connection pooling is disabled because these tests dynamically alter the database schema (creating/dropping tables, applying/reverting migrations). With pooling enabled, pooled connections might hold stale schema information or cached state that conflicts with the schema changes made during migration operations.

However, if you think pooling should work fine for these tests, I can remove the override and test it.

- Implement IAsyncLifetime and call Fixture.ReseedAsync() in InitializeAsync
  instead of manually calling CleanDatabase in each test
- Use context.Database.OpenConnection/CloseConnection instead of direct
  connection.Open/Close calls
- Move database cleanup logic to fixture's CleanAsync override
- Add GetTableNamesAsync to fixtures for async cleanup
@thromel

This comment was marked as resolved.

- Simplify CSharpMigrationCompiler.GetMetadataReferences to use cached
  references plus context assembly, removing explicit Assembly.Load calls
- Remove duplicate name validation from AddMigration/AddAndApplyMigration
  since PrepareForMigration already validates
- Remove UsePooling override (controls DbContext pooling, not connection pooling)
@thromel
Copy link
Contributor Author

thromel commented Jan 9, 2026

Addressed the remaining unresolved comments:

  1. CSharpMigrationCompiler - Simplified GetMetadataReferences to use cached references from loaded assemblies plus context assembly. Removed explicit Assembly.Load() calls since assemblies should already be loaded by that point.

  2. MigrationsOperations - Removed duplicate name validation from AddMigration and AddAndApplyMigration since PrepareForMigration already validates.

  3. UsePooling - Removed the UsePooling => false override since it controls DbContext pooling, not connection pooling.

Commit: e7b6329

The validation must happen BEFORE CreateContext() because:
1. Tests use mock contexts that can't be created without a database
2. PrepareForMigration runs AFTER context creation
3. Without early validation, context creation fails with a confusing error
   instead of the expected "A migration name must be specified" message

This validation is not truly duplicated - it's needed at this specific
point in the call sequence to ensure proper error messages.
The simplified version wasn't loading the context assembly's referenced
assemblies explicitly. Static references may not be loaded into memory
when the cache is built, causing compilation failures when those
assemblies are needed.
The IAsyncLifetime + ReseedAsync approach was causing CI failures.
Reverting to the original CleanDatabase method approach that was
working in commit cd324fd.

This will need to be addressed differently based on reviewer feedback.
- Implement IAsyncLifetime on test class, call Fixture.ReseedAsync()
- Move GetTableNames and CleanDatabase to fixture classes
- Use context.Database.OpenConnection/CloseConnection instead of direct connection calls
- Remove per-test CleanDatabase calls (handled by ReseedAsync)
The SharedStoreFixtureBase.ReseedAsync() calls CleanAsync which calls Clean.
Override Clean directly to properly integrate with the base class cleanup flow.
Comment on lines +335 to +338
if (string.IsNullOrWhiteSpace(name))
{
throw new OperationException(DesignStrings.MigrationNameRequired);
}
Copy link
Member

Choose a reason for hiding this comment

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

Ok, then remove this check since it's already checked before this call

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Allow to create a migration and apply it without recompiling

2 participants