.NET Aspire SQL Database Projects hosting integration

Includes: Hosting integration not Client integration

Note

This integration is part of the .NET Aspire Community Toolkit and isn't officially supported by the .NET Aspire team.

In this article, you learn how to use the .NET Aspire SQL Database Projects hosting integration to publish your database schema to your SQL Server database.

Prerequisites

This integration requires a SQL Database Project based on either MSBuild.Sdk.SqlProj or Microsoft.Build.Sql.

Hosting integration

To get started with the .NET Aspire SQL Database Projects hosting integration, install the 📦 CommunityToolkit.Aspire.Hosting.SqlDatabaseProjects NuGet package in the app host project.

dotnet add package CommunityToolkit.Aspire.Hosting.SqlDatabaseProjects

For more information, see dotnet add package or Manage package dependencies in .NET applications.

Example usage

Add a reference to the 📦 MSBuild.Sdk.SqlProj or 📦 Microsoft.Build.Sql project you want to publish in your .NET Aspire app host project:

dotnet add reference ../MySqlProj/MySqlProj.csproj

Note

Adding this reference will currently result in warning ASPIRE004 on the project due to how references are parsed. The .NET Aspire team is aware of this and we're working on a cleaner solution.

Add the project as a resource to your .NET Aspire AppHost:

var builder = DistributedApplication.CreateBuilder(args);

var sql = builder.AddSqlServer("sql")
                 .AddDatabase("test");

builder.AddSqlProject<Projects.MySqlProj>("mysqlproj")
       .WithReference(sql);

Now when you run your .NET Aspire app host project you see the SQL Database Project being published to the specified SQL Server.

Local .dacpac file support

If you are sourcing your .dacpac file from somewhere other than a project reference, you can also specify the path to the .dacpac file directly:

var builder = DistributedApplication.CreateBuilder(args);

var sql = builder.AddSqlServer("sql")
                 .AddDatabase("test");

builder.AddSqlProject("mysqlproj")
       .WithDacpac("path/to/mysqlproj.dacpac")
       .WithReference(sql);

Deployment options support

To define options that affect the behavior of package deployment, call the WithConfigureDacDeployOptions API:

var builder = DistributedApplication.CreateBuilder(args);

var sql = builder.AddSqlServer("sql")
                 .AddDatabase("test");

builder.AddSqlProject("mysqlproj")
       .WithConfigureDacDeployOptions(options => options.IncludeCompositeObjects = true)
       .WithReference(sql);

builder.Build().Run();

The preceding code:

  • Adds a SQL server resource named sql and adds a test database resource to it.
  • Adds a SQL project resource named mysqlproj and then configures the DacDeployOptions.
  • The SQL project resource depends on the database resource.

Redeploy support

If you make changes to your SQL Database project while the app host is running, you can use the Redeploy custom action on the .NET Aspire dashboard to redeploy your updates without having to restart the app host.

See also