Azure SQL Hosting integration reference
Este conteúdo não está disponível em sua língua ainda.
To get started with the Aspire Azure SQL integrations, follow the Get started with Azure SQL integrations guide.
This article includes full details about the Aspire Azure SQL Hosting integration, which models Azure SQL server and database resources as the AzureSqlServerResource and AzureSqlDatabaseResource types. To access these types and APIs, you need to install the Azure SQL Hosting integration in your AppHost project.
Installation
Section titled “Installation”The Aspire Azure SQL Database hosting integration models the SQL Server as the AzureSqlServerResource type and SQL databases as the AzureSqlDatabaseResource type. To access these types and APIs for expressing them within your AppHost project, install the 📦 Aspire.Hosting.Azure.Sql NuGet package:
aspire add azure-sqlA Aspire CLI é interativa; escolha o resultado adequado quando solicitado:
Select an integration to add:
> azure-sql (Aspire.Hosting.Azure.Sql)> Other results listed as selectable options...#:package Aspire.Hosting.Azure.Sql@*<PackageReference Include="Aspire.Hosting.Azure.Sql" Version="*" />Add Azure SQL server resource and database resource
Section titled “Add Azure SQL server resource and database resource”In your AppHost project, call AddAzureSqlServer to add and return an Azure SQL server resource builder. Chain a call to the returned resource builder to AddDatabase, to add an Azure SQL database resource:
var builder = DistributedApplication.CreateBuilder(args);
var azureSql = builder.AddAzureSqlServer("azuresql") .AddDatabase("database");
var myService = builder.AddProject<Projects.MyService>() .WithReference(azureSql);The preceding call to AddAzureSqlServer configures the Azure SQL server resource to be deployed as an Azure SQL Database server.
Connect to an existing Azure SQL server
Section titled “Connect to an existing Azure SQL server”You might have an existing Azure SQL Database service that you want to connect to. You can chain a call to annotate that your AzureSqlServerResource is an existing resource:
var builder = DistributedApplication.CreateBuilder(args);
var existingSqlServerName = builder.AddParameter("existingSqlServerName");var existingSqlServerResourceGroup = builder.AddParameter("existingSqlServerResourceGroup");
var sqlserver = builder.AddAzureSqlServer("sqlserver") .AsExisting(existingSqlServerName, existingSqlServerResourceGroup) .AddDatabase("database");
builder.AddProject<Projects.ExampleProject>() .WithReference(sqlserver);
// After adding all resources, run the app...For more information on treating Azure SQL resources as existing resources, see Use existing Azure resources.
Run Azure SQL server resource as a container
Section titled “Run Azure SQL server resource as a container”The Azure SQL Server hosting integration supports running the Azure SQL server as a local container. This is beneficial for situations where you want to run the Azure SQL server locally for development and testing purposes, avoiding the need to provision an Azure resource or connect to an existing Azure SQL server.
To run the Azure SQL server as a container, call the RunAsContainer method:
var builder = DistributedApplication.CreateBuilder(args);
var azureSql = builder.AddAzureSqlServer("azuresql") .RunAsContainer();
var azureSqlData = azureSql.AddDatabase("database");
var exampleProject = builder.AddProject<Projects.ExampleProject>() .WithReference(azureSqlData);The preceding code configures an Azure SQL Database resource to run locally in a container.