SQL Database Projects integration
The Aspire SQL Database Projects hosting integration enables you to deploy SQL database schemas from SQL projects or DACPAC files during local development and testing. This integration supports both MSBuild.Sdk.SqlProj and Microsoft.Build.Sql project formats.
Hosting integration
Section titled “Hosting integration”To get started with the Aspire SQL Database Projects hosting integration, install the CommunityToolkit.Aspire.Hosting.SqlDatabaseProjects NuGet package in the app host project.
aspire add communitytoolkit-sqldatabaseprojectsThe Aspire CLI is interactive, be sure to select the appropriate search result when prompted:
Select an integration to add:
> communitytoolkit-sqldatabaseprojects (CommunityToolkit.Aspire.Hosting.SqlDatabaseProjects)> Other results listed as selectable options...#:package CommunityToolkit.Aspire.Hosting.SqlDatabaseProjects@*<PackageReference Include="CommunityToolkit.Aspire.Hosting.SqlDatabaseProjects" Version="*" />Add SQL project
Section titled “Add SQL project”To deploy a SQL project to a SQL Server database, use the AddSqlProject extension method:
var builder = DistributedApplication.CreateBuilder(args);
var sql = builder.AddSqlServer("sql") .AddDatabase("sqldb");
sql.AddSqlProject<Projects.DatabaseProject>("database-project");
builder.AddProject<Projects.ExampleProject>() .WithReference(sql);
// After adding all resources, run the app...This method takes a project reference to your SQL database project and deploys it to the specified database when the app host starts.
Add SQL package (NuGet)
Section titled “Add SQL package (NuGet)”To deploy a SQL database schema from a NuGet package, use the AddSqlPackage extension method:
var builder = DistributedApplication.CreateBuilder(args);
var sql = builder.AddSqlServer("sql") .AddDatabase("sqldb");
sql.AddSqlPackage("Contoso.DatabasePackage", "1.0.0");
builder.AddProject<Projects.ExampleProject>() .WithReference(sql);
// After adding all resources, run the app...This method downloads the specified NuGet package and deploys the DACPAC it contains to the database.
Add DACPAC file
Section titled “Add DACPAC file”To deploy a DACPAC file directly, use the WithDacpac extension method:
var builder = DistributedApplication.CreateBuilder(args);
var sql = builder.AddSqlServer("sql") .AddDatabase("sqldb");
sql.WithDacpac("path/to/database.dacpac");
builder.AddProject<Projects.ExampleProject>() .WithReference(sql);
// After adding all resources, run the app...Configure DAC deployment options
Section titled “Configure DAC deployment options”You can customize the DACPAC deployment behavior using the WithConfigureDacDeployOptions method:
var builder = DistributedApplication.CreateBuilder(args);
var sql = builder.AddSqlServer("sql") .AddDatabase("sqldb");
sql.AddSqlProject<Projects.DatabaseProject>("database-project") .WithConfigureDacDeployOptions(options => { options.BlockOnPossibleDataLoss = false; options.DropObjectsNotInSource = true; });
// After adding all resources, run the app...Redeploy action
Section titled “Redeploy action”The SQL Database Projects integration adds a custom “Redeploy” action to the Aspire dashboard. This action allows you to manually trigger a redeployment of the database schema without restarting the entire app host.
To use the Redeploy action:
- Open the Aspire dashboard
- Find your SQL database resource
- Click the “Redeploy” action in the resource’s menu
This is useful during development when you make changes to your database schema and want to quickly apply them.
Multiple SQL projects
Section titled “Multiple SQL projects”You can add multiple SQL projects to the same database or different databases:
var builder = DistributedApplication.CreateBuilder(args);
var sql = builder.AddSqlServer("sql");
var db1 = sql.AddDatabase("db1");db1.AddSqlProject<Projects.Database1Project>("db1-project");
var db2 = sql.AddDatabase("db2");db2.AddSqlProject<Projects.Database2Project>("db2-project");
// After adding all resources, run the app...