Skip to content

SQL Database Projects integration

⭐ Community Toolkit SQL Database Projects icon

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.

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 CLI — Add CommunityToolkit.Aspire.Hosting.SqlDatabaseProjects package
aspire add communitytoolkit-sqldatabaseprojects

The Aspire CLI is interactive, be sure to select the appropriate search result when prompted:

Aspire CLI — Example output prompt
Select an integration to add:
> communitytoolkit-sqldatabaseprojects (CommunityToolkit.Aspire.Hosting.SqlDatabaseProjects)
> Other results listed as selectable options...

To deploy a SQL project to a SQL Server database, use the AddSqlProject extension method:

C# — AppHost.cs
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.

To deploy a SQL database schema from a NuGet package, use the AddSqlPackage extension method:

C# — AppHost.cs
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.

To deploy a DACPAC file directly, use the WithDacpac extension method:

C# — AppHost.cs
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...

You can customize the DACPAC deployment behavior using the WithConfigureDacDeployOptions method:

C# — AppHost.cs
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...

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:

  1. Open the Aspire dashboard
  2. Find your SQL database resource
  3. 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.

You can add multiple SQL projects to the same database or different databases:

C# — AppHost.cs
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...
FAQCollaborateCommunityDiscussWatch