İçeriğe geç

Azure SQL Database hosting integration

Bu içerik henüz dilinizde mevcut değil.

Azure SQL Database logo

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 CLI — Aspire.Hosting.Azure.Sql paketi ekle
aspire add azure-sql

Aspire CLI etkileşimlidir; istendiğinde uygun sonucu seçin:

Aspire CLI — Örnek çıktı
Select an integration to add:
> azure-sql (Aspire.Hosting.Azure.Sql)
> Other results listed as selectable options...

For an introduction to working with the Azure SQL Database hosting integration, see Get started with the Azure SQL Database integration.

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:

C# — AppHost.cs
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.

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:

C# — AppHost.cs
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:

C# — AppHost.cs
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.

When you reference Azure SQL Server resources using WithReference, the following connection properties are made available to the consuming project:

The Azure SQL Server resource exposes the following connection properties:

Property NameDescription
HostThe fully qualified domain name of the Azure SQL Server
PortThe SQL Server port (1433 for Azure)
UriThe connection URI, with the format mssql://{Host}:{Port}
JdbcConnectionStringJDBC connection string with the format jdbc:sqlserver://{Host}:{Port};encrypt=true;trustServerCertificate=false

The Azure SQL database resource inherits all properties from its parent Azure SQL Server resource and adds:

Property NameDescription
DatabaseNameThe name of the database
UriThe connection URI, with the format mssql://{Host}:{Port}/{DatabaseName}
JdbcConnectionStringJDBC connection string with the format jdbc:sqlserver://{Host}:{Port};database={DatabaseName};encrypt=true;trustServerCertificate=false