-
Notifications
You must be signed in to change notification settings - Fork 3.4k
Add runtime migration creation and application #37415
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
AndriySvyryd
merged 49 commits into
dotnet:main
from
thromel:feature/runtime-migrations
Jan 16, 2026
Merged
Add runtime migration creation and application #37415
AndriySvyryd
merged 49 commits into
dotnet:main
from
thromel:feature/runtime-migrations
Jan 16, 2026
+2,195
−79
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
a15611a to
9a35a9b
Compare
62018f1 to
5c41f2f
Compare
test/EFCore.SqlServer.FunctionalTests/Migrations/RuntimeMigrationSqlServerTest.cs
Outdated
Show resolved
Hide resolved
src/EFCore.Design/Design/DesignTimeServiceCollectionExtensions.cs
Outdated
Show resolved
Hide resolved
src/EFCore.Design/Migrations/Design/IDynamicMigrationsAssembly.cs
Outdated
Show resolved
Hide resolved
src/EFCore.Design/Migrations/Design/IDynamicMigrationsAssembly.cs
Outdated
Show resolved
Hide resolved
AndriySvyryd
reviewed
Jan 2, 2026
AndriySvyryd
reviewed
Jan 2, 2026
src/EFCore.Relational/Extensions/RelationalDatabaseFacadeExtensions.cs
Outdated
Show resolved
Hide resolved
AndriySvyryd
reviewed
Jan 2, 2026
AndriySvyryd
reviewed
Jan 2, 2026
AndriySvyryd
reviewed
Jan 2, 2026
AndriySvyryd
reviewed
Jan 2, 2026
This comment was marked as outdated.
This comment was marked as outdated.
AndriySvyryd
reviewed
Jan 6, 2026
test/EFCore.Design.Tests/Design/Internal/MigrationsOperationsTest.cs
Outdated
Show resolved
Hide resolved
AndriySvyryd
reviewed
Jan 6, 2026
AndriySvyryd
reviewed
Jan 6, 2026
AndriySvyryd
reviewed
Jan 6, 2026
test/EFCore.Design.Tests/Design/Internal/MigrationsOperationsTest.cs
Outdated
Show resolved
Hide resolved
src/EFCore.Relational/Migrations/Internal/MigrationsAssembly.cs
Outdated
Show resolved
Hide resolved
test/EFCore.Design.Tests/Migrations/Design/CSharpMigrationCompilerTest.cs
Outdated
Show resolved
Hide resolved
test/EFCore.Design.Tests/Migrations/Design/CSharpMigrationCompilerTest.cs
Outdated
Show resolved
Hide resolved
249ae47 to
6b86657
Compare
src/EFCore.Relational/Migrations/Internal/MigrationsAssembly.cs
Outdated
Show resolved
Hide resolved
src/EFCore.Relational/Migrations/Internal/MigrationsAssembly.cs
Outdated
Show resolved
Hide resolved
AndriySvyryd
approved these changes
Jan 16, 2026
Member
AndriySvyryd
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for your contribution!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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
The
-o/--output-dir,-n/--namespace, and--jsonoptions require--addto be specified.PowerShell Usage
Architecture
IMigrationCompiler/CSharpMigrationCompilerIMigrationsAssembly.AddMigrations(Assembly)MigrationsOperations.AddAndApplyMigration()Design Decisions
IMigrationsScaffolderfor scaffolding andIMigratorfor applying, adding only the newIMigrationCompilerserviceIMigrationsAssemblyinterface to accept additional assemblies containing runtime-compiled migrationsAddMigration, files are always saved to enable source control and future recompilationIMigrationCompilerandCSharpMigrationCompilerare in the.Internalnamespace as they require design work for public APIMigrationsAssemblyuses locking to protect against race conditions when adding migrations concurrentlyWorkflow
Robustness Features
AddAndApplyMigrationwraps the save-compile-register-apply chain in try-catch, deleting saved files on failure to prevent orphansPrepareForMigrationensures the DbContext is disposed if validation or service building failsMigrationsAssemblyuses locking to protect shared state (migrations dictionary, model snapshot, additional assemblies list)Limitations
[RequiresDynamicCode]Test plan
CSharpMigrationCompilerMigrationsOperations.AddAndApplyMigrationRuntimeMigrationTestBase(SQLite and SQL Server implementations)Fixes #37342