This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
Note
Access to this page requires authorization. You can trysigning in orchanging directories.
Access to this page requires authorization. You can trychanging directories.
Includes: Hosting integration only —
Client integration not included
Note
This integration is part of theAspire Community Toolkit andisn't officially supported by the Aspire team.
In this article, you learn how to use the Aspire SQL Database Projects hosting integration to publish your database schema to your SQL Server database.
This integration requires a SQL Database Project based on eitherMSBuild.Sdk.SqlProj orMicrosoft.Build.Sql.
To get started with the Aspire SQL Database Projects hosting integration, install the📦 CommunityToolkit.Aspire.Hosting.SqlDatabaseProjects NuGet package in the AppHost project.
dotnet add package CommunityToolkit.Aspire.Hosting.SqlDatabaseProjectsFor more information, seedotnet add package orManage package dependencies in .NET applications.
Add a reference to the📦 MSBuild.Sdk.SqlProj or📦 Microsoft.Build.Sql project you want to publish in your Aspire AppHost project:
dotnet add reference ../MySqlProj/MySqlProj.csprojNote
Adding this reference will currently result in warningASPIRE004 on the project due to how references are parsed. The Aspire team is aware of this and we're working on a cleaner solution.
Add the project as a resource to your 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 Aspire AppHost project you see the SQL Database Project being published to the specified SQL Server.
Starting with version 9.2.0, you can deploy databases from referenced NuGet packages, such as those produced by📦 MSBuild.Sdk.SqlProj or📦 Microsoft.Build.Sql. To deploy, add the NuGet package to your Aspire AppHost project, for example:
dotnet add package ErikEJ.Dacpac.ChinookNext, edit your project file to set theIsAspirePackageResource flag toTrue for the correspondingPackageReference, as shown in the following example:
<PackageReference Include="ErikEJ.Dacpac.Chinook" Version="1.0.0" IsAspirePackageResource="True" />Finally, add the package as a resource to your app model:
var builder = DistributedApplication.CreateBuilder(args);var sql = builder.AddSqlServer("sql") .AddDatabase("test");builder.AddSqlPackage<Packages.ErikEJ_Dacpac_Chinook>("chinook") .WithReference(sql);Note
By default, the.dacpac is expected to be located undertools/<package-id>.dacpac. In the preceding example, thetools/ErikEJ.Dacpac.Chinook.dacpac path is expected. If for whatever reason the.dacpac is under a different path within the package you can useWithDacpac("relative/path/to/some.dacpac") API to specify a path relative to the root of AppHost project directory.
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);Starting with version 9.2.0, you can publish the SQL Database project to an existing SQL Server instance by using a connection string:
var builder = DistributedApplication.CreateBuilder(args);// Get an existing SQL Server connection string from the configurationvar connection = builder.AddConnectionString("Aspire");builder.AddSqlProject<Projects.SdkProject>("mysqlproj") .WithReference(connection);builder.Build().Run();To define options that affect the behavior of package deployment, call theWithConfigureDacDeployOptions 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:
sql and adds atest database resource to it.mysqlproj and then configures theDacDeployOptions.If you make changes to your SQL Database project while the AppHost is running, you can use theRedeploy custom action on the Aspire dashboard to redeploy your updates without having to restart the AppHost.
Was this page helpful?
Need help with this topic?
Want to try using Ask Learn to clarify or guide you through this topic?
Was this page helpful?
Want to try using Ask Learn to clarify or guide you through this topic?