Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
In this tutorial, you learn to configure an ASP.NET Core app with a SQL Server Database for deployment to Azure. Aspire provides multiple SQL Server integration configurations that provision different database services in Azure. You'll learn how to:
- Create a basic ASP.NET Core app that is configured to use the Aspire SQL Server integration
- Configure the app to provision an Azure SQL Database
- Configure the app to provision a containerized SQL Server database
Note
This document focuses specifically on Aspire configurations to provision and deploy SQL Server resources in Azure. Visit the Azure Container Apps deployment tutorial to learn more about the full Aspire deployment process.
Prerequisites
To work with Aspire, you need the following installed locally:
- .NET 8.0 or .NET 9.0.
- Starting with Aspire 9.4, .NET 10 Preview 5 or later is supported.
- An OCI compliant container runtime, such as:
- Docker Desktop or Podman. For more information, see Container runtime.
- An Integrated Developer Environment (IDE) or code editor, such as:
- Visual Studio 2022 version 17.9 or higher (Optional)
- Visual Studio Code (Optional)
- C# Dev Kit: Extension (Optional)
- JetBrains Rider with Aspire plugin (Optional)
For more information, see Aspire setup and tooling, and Aspire SDK.
Create the sample solution
- At the top of Visual Studio, navigate to File > New > Project.
- In the dialog window, search for Aspire and select Aspire Starter App. Choose Next.
- On the Configure your new project screen:
- Enter a Solution name of AspireSql.
- Leave the rest of the values at their defaults and select Next.
- On the Additional information screen:
- In the Framework list, verify that .NET 9.0 is selected.
- In the Aspire version list, verify that 9.1 is selected.
- Choose Create.
Visual Studio creates a new ASP.NET Core solution that is structured to use Aspire. The solution consists of the following projects:
- AspireSql.ApiService: An API project that depends on service defaults.
- AspireSql.AppHost: An orchestrator project designed to connect and configure the different projects and services of your app. The orchestrator should be set as the startup project.
- AspireSql.ServiceDefaults: A shared class library to hold configurations that can be reused across the projects in your solution.
- AspireSql.Web: A Blazor project that depends on service defaults.
Configure the app for SQL Server deployment
Aspire provides two built-in configuration options to streamline SQL Server deployment on Azure:
- Provision a containerized SQL Server database using Azure Container Apps
- Provision an Azure SQL Database instance
Add the Aspire integration to the app
Add the appropriate Aspire integration to the AspireSql.AppHost project for your desired hosting service.
Open a command prompt and add the 📦 Aspire.Hosting.Azure.Sql NuGet package to the AspireSql.AppHost project:
cd AspireSql.AppHost
dotnet add package Aspire.Hosting.Azure.Sql
Configure the AppHost project
Configure the AspireSql.AppHost project for your desired SQL database service.
Replace the contents of the Program.cs file in the AspireSql.AppHost project with the following code:
var builder = DistributedApplication.CreateBuilder(args);
var apiService = builder.AddProject<Projects.AspireSql_ApiService>("apiservice");
// Provisions an Azure SQL Database when published
var sqlServer = builder.AddAzureSqlServer("sqlserver")
.AddDatabase("sqldb");
builder.AddProject<Projects.AspireSql_Web>("webfrontend")
.WithExternalHttpEndpoints()
.WithReference(apiService)
.WaitFor(apiService);
builder.Build().Run();
The preceding code adds an Azure SQL Server resource to your app and configures a connection to a database called sqldb. The AddAzureSqlServer method ensures that tools such as the Azure Developer CLI or Visual Studio create an Azure SQL Database resource during the deployment process.
Deploy the app
The aspire deploy command supports Aspire SQL Server integration configurations to streamline deployments. The command consumes these settings and provisions properly configured resources for you.
To deploy your app to Azure Container Apps, run the following command from the AspireSql.AppHost directory:
aspire deploy
When you run the aspire deploy command for the first time, you'll be prompted to:
- Sign in to Azure: Follow the authentication prompts to sign in to your Azure account.
- Select a subscription: Choose the Azure subscription you want to use for deployment.
- Select or create a resource group: Choose an existing resource group or create a new one.
- Select a location: Choose the Azure region where you want to deploy your resources.
The deployment process will provision the necessary Azure resources and deploy your Aspire app.
The deployment process provisioned an Azure SQL Database resource due to the .AppHost configuration you provided.
Clean up resources
Run the following Azure CLI command to delete the resource group when you no longer need the Azure resources you created. Deleting the resource group also deletes the resources contained inside of it.
az group delete --name <your-resource-group-name>
For more information, see Clean up resources in Azure.