I've successfully created a .NET Aspire integration for DBMigrator that allows automatic database migrations to run as part of the Aspire application lifecycle.
This package provides the Aspire hosting integration:
Location: DBMigrator.Aspire.Hosting/
Files Created:
DBMigrator.Aspire.Hosting.csproj- Project fileDBMigratorResource.cs- Resource implementationDBMigratorResourceBuilderExtensions.cs- Fluent API for configurationDBMigratorAnnotations.cs- Configuration annotationsDBMigratorLifecycleHook.cs- Lifecycle hook for executionDBMigratorHostingExtensions.cs- Service registrationREADME.md- Package documentationARCHITECTURE.md- Technical architecture documentationNuGet.config- Package source configuration
A complete working example showing how to use the integration:
Location: Examples/DBMigrator.Aspire.Example/
Files Created:
DBMigrator.Aspire.Example.csproj- Example AppHost projectProgram.cs- Example configurationappsettings.json- Logging configurationREADME.md- Example documentation- Sample migration scripts in
Migrations/1.0/UserManagement/Migrations/
Unit tests for the integration:
Location: DBMigrator.Aspire.Tests/
Files Created:
DBMigrator.Aspire.Tests.csproj- Test projectDBMigratorResourceBuilderExtensionsTests.cs- Unit tests
- Updated main
README.mdwith Aspire integration section - Created comprehensive architecture documentation
var builder = DistributedApplication.CreateBuilder(args);
var sqlServer = builder.AddSqlServer("sql");
var database = sqlServer.AddDatabase("mydb");
var dbMigrator = builder.AddDBMigrator("dbmigrator", database, "./Migrations")
.WithTargetVersion("2.0") // Optional: target specific version
.WithSkipValidation() // Optional: skip validation
.WithDryRun(); // Optional: test without committing
builder.AddDBMigratorLifecycleHook();
var api = builder.AddProject<Projects.MyApi>("api")
.WaitForDBMigrator(dbMigrator); // Wait for migrations
builder.Build().Run();- Migrations run automatically before application starts
- Integrates with Aspire's lifecycle hooks
- Waits for SQL Server to be ready
- Reports success/failure through logging
- Target Version: Migrate to specific version or latest
- Skip Validation: Skip database integrity checks
- Dry Run: Test migrations without committing
- Dependency Management: Other resources can wait for migrations
- Uses
IDistributedApplicationLifecycleHookfor execution - Implements
IResourceandIResourceWithConnectionString - Annotations pattern for configuration
- Fluent API for developer experience
- Application starts ? Aspire initializes resources
- SQL Server container starts
DBMigratorLifecycleHook.BeforeStartAsync()triggered- Wait for SQL Server readiness
- Load configuration from annotations
- Connect to database and read migration scripts
- Calculate pending migrations
- Validate database (if not skipped)
- Execute migrations
- Log results
- Application resources start
- Migration failures prevent application startup
- Clear error messages with full stack traces
- Integration with Aspire dashboard for visibility
- Configuration errors detected early
var dbMigrator = builder.AddDBMigrator("dbmigrator", database, "./Migrations")
.WithSkipValidation() // Faster startup in dev
.WithDryRun(); // Test without changesvar dbMigrator = builder.AddDBMigrator("dbmigrator", database, "./Migrations");
builder.AddDBMigratorLifecycleHook();
// Full validation and migrationvar db1Migrator = builder.AddDBMigrator("migrator1", db1, "./Migrations/DB1");
var db2Migrator = builder.AddDBMigrator("migrator2", db2, "./Migrations/DB2");
builder.AddDBMigratorLifecycleHook();? Automatic migrations - No manual migration steps
? Type-safe configuration - Fluent C# API
? Integrated logging - See migrations in Aspire dashboard
? Local development - Works with local SQL Server containers
? Dependency management - Apps wait for migrations
? Consistent deployment - Same process everywhere
? Validation built-in - Prevents manual changes
? Dry run testing - Test before committing
? Comprehensive logging - Audit trail of changes
? Error handling - Fails fast on problems
dbmigrator upgrade -s server -d database -u user -p pass -f ./Migrationsbuilder.AddDBMigrator("dbmigrator", database, "./Migrations");
builder.AddDBMigratorLifecycleHook();The migrations now run automatically as part of application startup!
Tests cover:
- Resource creation and configuration
- Annotation application
- Fluent API chaining
- Dependency management
Can be tested with Aspire.Hosting.Testing:
var appHost = await DistributedApplicationTestingBuilder
.CreateAsync<Projects.TestAppHost>();
await using var app = await appHost.BuildAsync();
await app.StartAsync();
// Verify migrations ran successfullyThe integration can be published as a NuGet package:
- Package ID:
DBMigrator.Aspire.Hosting - Description: .NET Aspire integration for DBMigrator
- Tags: aspire, dbmigrator, database, migration
- Target Framework: .NET 9.0
Potential improvements:
- Health check integration
- Enhanced dashboard visualization
- Rollback/downgrade support in Aspire
- Parallel migration execution
- Azure SQL authentication support
- Migration history UI
Comprehensive documentation created:
DBMigrator.Aspire.Hosting/README.md- User guideDBMigrator.Aspire.Hosting/ARCHITECTURE.md- Technical detailsExamples/DBMigrator.Aspire.Example/README.md- Example walkthrough- Main
README.mdupdated with Aspire section
- .NET Version: 9.0
- .NET Aspire: 9.0 or later
- SQL Server: Any version supported by DBMigrator
- Existing DBMigrator: Fully compatible with existing migrations
To start using the integration:
-
Install the package (once published):
dotnet add package DBMigrator.Aspire.Hosting
-
Update your AppHost:
var dbMigrator = builder.AddDBMigrator("dbmigrator", database, "./Migrations"); builder.AddDBMigratorLifecycleHook();
-
Run your Aspire app:
dotnet run
-
See migrations in action:
- Open Aspire dashboard
- View DBMigrator logs
- Verify migrations applied
Total files created: 17
- Project and configuration files
- Resource implementation
- Extension methods
- Documentation
- Example AppHost
- Sample migrations
- Documentation
- Test project
- Unit tests
- Main README.md with Aspire integration section
The DBMigrator Aspire integration provides a seamless, developer-friendly way to handle database migrations in .NET Aspire applications. It maintains all the power and validation capabilities of DBMigrator while automating the execution as part of the application lifecycle.
The integration follows Aspire's patterns and conventions, making it feel like a natural extension of the platform. Developers can configure migrations using a fluent C# API, see results in the Aspire dashboard, and ensure their databases are always in the correct state before their applications start.