-
Notifications
You must be signed in to change notification settings - Fork 757
Add Aspire.Hosting.EntityFrameworkCore hosting integration for EF Core migration management #13481
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
base: main
Are you sure you want to change the base?
Conversation
…anagement - Create EFMigrationResource with IResourceWithWaitSupport - Add AddEFMigrations extension methods for project resources - Implement 6 resource commands: Update, Drop, Reset, Add, Remove, Status - Add RunDatabaseUpdateOnStart and PublishAs* configuration methods - Add EFCoreOperationExecutor for command execution - Create comprehensive test suite with 36 passing tests - Add detailed implementation checklist documentation 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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@copilot Continue implementation according to the checklist
src/Aspire.Hosting.EFCoreCommands/EFMigrationsBuilderExtensions.cs
Outdated
Show resolved
Hide resolved
src/Aspire.Hosting.EFCoreCommands/EFMigrationsBuilderExtensions.cs
Outdated
Show resolved
Hide resolved
- Reorganize README.md with Prerequisites section first, add PublishAsMigrationBundle example - Move "Running migrations on startup" to separate subsection after Resource Commands - Update prerequisites to recommend manual project file reference for Microsoft.EntityFrameworkCore.Design - Add TODO comment for documentation link update - Add remarks about multiple calls with different context types in extension methods - Fix error message format for duplicate context registration - Update implementation checklist with IDistributedApplicationEventingSubscriber note - Add EFCoreCommands demo to SqlServerEndToEnd playground Co-authored-by: AndriySvyryd <6539701+AndriySvyryd@users.noreply.github.com>
… dialog - Create EFMigrationEventSubscriber using IDistributedApplicationEventingSubscriber - Implement resource state transitions (Pending → Running → Finished/FailedToStart) - Add IInteractionService.PromptInputAsync for migration name prompt - Support custom migration name parameter in AddMigrationAsync - Update implementation checklist with links and completion status - Add test for event subscriber registration - All 37 tests pass Co-authored-by: AndriySvyryd <6539701+AndriySvyryd@users.noreply.github.com>
- Update code example to use GetService instead of GetRequiredService - Add fallback logic example matching actual implementation Co-authored-by: AndriySvyryd <6539701+AndriySvyryd@users.noreply.github.com>
src/Aspire.Hosting.EFCoreCommands/EFMigrationsBuilderExtensions.cs
Outdated
Show resolved
Hide resolved
src/Aspire.Hosting.EFCoreCommands/EFMigrationsBuilderExtensions.cs
Outdated
Show resolved
Hide resolved
playground/SqlServerEndToEnd/SqlServerEndToEnd.AppHost/Program.cs
Outdated
Show resolved
Hide resolved
- Create IEFMigrationResourceBuilder interface that stores ContextTypeName - Create EFMigrationResourceBuilder implementation wrapping inner builder - Update AddEFMigrations to return IEFMigrationResourceBuilder - Add string overload AddEFMigrations(name, contextTypeName) for runtime discovery - Add ContextTypeName property to EFMigrationResource - Add recompilation notification after migration is added (via logs) - Add 5 new tests for string context type overload - All 42 tests pass Co-authored-by: AndriySvyryd <6539701+AndriySvyryd@users.noreply.github.com>
src/Aspire.Hosting.EntityFrameworkCore/EFMigrationResourceBuilder.cs
Outdated
Show resolved
Hide resolved
- Rename method WithMigrationProject to WithMigrationsProject - Rename property MigrationProject to MigrationsProject in EFMigrationsOptions - Update all references in extensions, event subscriber, README, and tests - All 50 tests pass Co-authored-by: AndriySvyryd <6539701+AndriySvyryd@users.noreply.github.com>
src/Aspire.Hosting.EntityFrameworkCore/EFMigrationEventSubscriber.cs
Outdated
Show resolved
Hide resolved
The EFMigrationEventSubscriber now waits for the project resource (the one on which AddEFMigrations was called) to be healthy before attempting to apply migrations. This ensures the database connection is available when migrations run. - Use resourceNotificationService.WaitForResourceHealthyAsync before ApplyMigrationsAsync - Log message indicating waiting for project resource to be healthy - All 50 tests pass Co-authored-by: AndriySvyryd <6539701+AndriySvyryd@users.noreply.github.com>
| else if (executionContext.IsPublishMode) | ||
| { | ||
| // In publish mode, generate migration scripts/bundles | ||
| eventing.Subscribe<BeforePublishEvent>(OnBeforePublishAsync); |
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.
@copilot This should happen in a step not in an event.
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.
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.
@davidfowl I think copilot gave up on this PR
src/Aspire.Hosting.EntityFrameworkCore/EFCoreOperationExecutor.cs
Outdated
Show resolved
Hide resolved
src/Aspire.Hosting.EntityFrameworkCore/EFCoreOperationExecutor.cs
Outdated
Show resolved
Hide resolved
src/Aspire.Hosting.EntityFrameworkCore/EFCoreOperationExecutor.cs
Outdated
Show resolved
Hide resolved
src/Aspire.Hosting.EntityFrameworkCore/EFMigrationEventSubscriber.cs
Outdated
Show resolved
Hide resolved
|
🚀 Dogfood this PR with:
curl -fsSL https://raw.githubusercontent.com/dotnet/aspire/main/eng/scripts/get-aspire-cli-pr.sh | bash -s -- 13481Or
iex "& { $(irm https://raw.githubusercontent.com/dotnet/aspire/main/eng/scripts/get-aspire-cli-pr.ps1) } 13481" |
|
|
||
| ```csharp | ||
| var startup = builder.AddProject<Projects.Api>("api"); | ||
| var dataProject = builder.AddProject<Projects.Data>("data"); |
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.
@davidfowl How can I mark a project resource as "non-runnable"?
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.
Pull request overview
This PR introduces a new hosting integration package Aspire.Hosting.EntityFrameworkCore that provides comprehensive EF Core migration management for Aspire AppHost projects.
Key additions:
- Extension methods (
AddEFMigrations<TContext>()with multiple overloads) to attach migration management to project resources - 6 dashboard resource commands (Update/Drop/Reset Database, Add/Remove Migration, Get Status) with interactive prompts
- Reflection-based executor using collectible AssemblyLoadContext for EF Core operations
- WaitFor support with health checks for migration dependencies
- Event-driven startup migrations and publish pipeline integration
- 50 comprehensive unit tests covering all functionality
Reviewed changes
Copilot reviewed 18 out of 18 changed files in this pull request and generated 2 comments.
Show a summary per file
| File | Description |
|---|---|
| src/Aspire.Hosting.EntityFrameworkCore/Aspire.Hosting.EntityFrameworkCore.csproj | New project file multi-targeting net8.0/9.0/10.0 with package validation disabled |
| src/Aspire.Hosting.EntityFrameworkCore/README.md | Hosting integration documentation following standard structure with usage examples |
| src/Aspire.Hosting.EntityFrameworkCore/EFMigrationResource.cs | Resource class implementing IResourceWithWaitSupport with configuration properties |
| src/Aspire.Hosting.EntityFrameworkCore/EFMigrationResourceBuilder.cs | Custom builder with fluent configuration methods (RunDatabaseUpdateOnStart, Publish*, etc.) |
| src/Aspire.Hosting.EntityFrameworkCore/EFResourceBuilderExtensions.cs | Extension methods for AddEFMigrations with 4 overloads and command registration |
| src/Aspire.Hosting.EntityFrameworkCore/EFMigrationHealthCheck.cs | Health check implementation for migration state monitoring |
| src/Aspire.Hosting.EntityFrameworkCore/EFMigrationEventSubscriber.cs | Event subscriber handling startup migrations via BeforeResourceStartedEvent |
| src/Aspire.Hosting.EntityFrameworkCore/EFCoreOperationExecutor.cs | Reflection-based EF operation executor using AssemblyLoadContext isolation |
| tests/Aspire.Hosting.EntityFrameworkCore.Tests/*.cs | Comprehensive test suite (50 tests) covering resource creation, configuration, commands, and WaitFor |
| playground/SqlServerEndToEnd/* | Example integration showing AddEFMigrations usage with WaitFor dependencies |
| Aspire.slnx | Solution file updated with new projects |
src/Aspire.Hosting.EntityFrameworkCore/EFMigrationHealthCheck.cs
Outdated
Show resolved
Hide resolved
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>


Description
New hosting integration package
Aspire.Hosting.EntityFrameworkCorethat provides EF Core migration management commands for Aspire AppHost projects.Key Features
AddEFMigrations<TContext>()and string-basedAddEFMigrations(name, contextTypeName)overloads.RunDatabaseUpdateOnStart(),PublishAsMigrationScript(),PublishAsMigrationBundle(),WithMigrationOutputDirectory(),WithMigrationNamespace(),WithMigrationsProject()EFMigrationResourceimplementsIResourceWithWaitSupport. WhenRunDatabaseUpdateOnStart()is called, a health check is registered that reports healthy only when migrations complete. Migrations wait for the project resource to be healthy before executing.WithMigrationsProject()when migrations are in a different project than the startup project. Both startup and target assemblies are loaded in the same AssemblyLoadContext.AssemblyLoadContextto load and execute EF Core operations, with proper unloading after each operation. Uses MSBuild for property resolution with configuration/runtime heuristics.IInteractionService.PromptInputAsyncfor Add Migration command with recompilation notification viaPromptNotificationAsync. Remove Migration also shows recompilation notification.IInteractionService.PromptMessageBoxAsyncto show current migration statusEFMigrationEventSubscriberhandlesAfterResourcesCreatedEventfor startup migrations (after project resource is healthy) andBeforePublishEventfor generating migration scripts/bundles during publishChecklist
Aspire.Hosting.EntityFrameworkCoreproject structureEFMigrationResourcewithIResourceWithWaitSupportEFMigrationResourceBuilderRunDatabaseUpdateOnStartwith event subscriber (waits for project resource to be healthy)EFCoreOperationExecutorusing collectible AssemblyLoadContextOriginal prompt
💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.