Movatterモバイル変換


[0]ホーム

URL:


Skip to main content

This browser is no longer supported.

Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.

Download Microsoft EdgeMore info about Internet Explorer and Microsoft Edge
Table of contentsExit editor mode

Aspire SQL Database Projects hosting integration

Feedback

In this article

Includes:Hosting integration included Hosting integration only —Client integration not included 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.

Prerequisites

This integration requires a SQL Database Project based on eitherMSBuild.Sdk.SqlProj orMicrosoft.Build.Sql.

Hosting integration

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.SqlDatabaseProjects

For more information, seedotnet add package orManage 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 Aspire AppHost project:

dotnet add reference ../MySqlProj/MySqlProj.csproj

Note

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.

NuGet Package support

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.Chinook

Next, 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.

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);

Support for existing SQL Server instances

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();

Deployment options support

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:

  • Adds a SQL server resource namedsql and adds atest database resource to it.
  • Adds a SQL project resource namedmysqlproj and then configures theDacDeployOptions.
  • The SQL project resource depends on the database resource.

Redeploy support

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.

See also

Collaborate with us on GitHub
The source for this content can be found on GitHub, where you can also create and review issues and pull requests. For more information, seeour contributor guide.

Feedback

Was this page helpful?

YesNoNo

Need help with this topic?

Want to try using Ask Learn to clarify or guide you through this topic?

Suggest a fix?

  • Last updated on

In this article

Was this page helpful?

YesNo
NoNeed help with this topic?

Want to try using Ask Learn to clarify or guide you through this topic?

Suggest a fix?