Skip to content

Commit 7f401b7

Browse files
Add Azure SQL integration articles (#3217)
* New article created and first sections added. * Hosting integration section complete. * Small corrections. * Placed hosting integration text into include so I can reuse it in EF article. * Both articles first draft complete. * Fixed some article links. * Added new articles to TOC. * Fixed one typo. * Fixed another typo. * Trying a fix for 'not included in build scope' warnings. * Fixed two markdown lint errors.
1 parent 5d43d20 commit 7f401b7

8 files changed

+555
-387
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
---
2+
title: .NET Aspire Azure SQL Entity Framework Core integration
3+
description: Learn how to use the .NET Aspire Azure SQL integration with Entity Framework Core, which includes both hosting and client integrations.
4+
ms.date: 04/30/2025
5+
uid: database/azure-sql-entity-framework-integration
6+
---
7+
8+
# .NET Aspire Azure SQL Entity Framework Core integration
9+
10+
[!INCLUDE [includes-hosting-and-client](../includes/includes-hosting-and-client.md)]
11+
12+
[Azure SQL](https://azure.microsoft.com/products/azure-sql) is a family of relational database management systems that run in the Azure cloud. The database systems are Platform-as-a-Service (PaaS) products that enable database administrators to implement highly scalable and available databases without maintaining complex infrastructures themselves. The .NET Aspire Azure SQL Server Hosting integration provides methods to create a new Azure Database server and databases from code in your .NET Aspire app host project. In a consuming project, you can use the .NET Aspire SQL Server Entity Framework Core client integration as you would for any other SQL Server instance.
13+
14+
## Hosting integration
15+
16+
[!INCLUDE [azure-sql-hosting](includes/azure-sql-hosting.md)]
17+
18+
## Client integration
19+
20+
[!INCLUDE [sql-server-ef-client](includes/sql-server-ef-client.md)]
21+
22+
## See also
23+
24+
- [Azure SQL Database](/azure/azure-sql/database)
25+
- [.NET Aspire database containers sample](/samples/dotnet/aspire-samples/aspire-database-containers/)
26+
- [.NET Aspire integrations](../fundamentals/integrations-overview.md)
27+
- [.NET Aspire GitHub repo](https://github.com/dotnet/aspire)
28+
- [Azure SQL & SQL Server Aspire Samples](https://github.com/Azure-Samples/azure-sql-db-aspire)
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
---
2+
title: .NET Aspire Azure SQL integration
3+
description: Learn how to use the .NET Aspire Azure SQL integration, which includes both hosting and client integrations.
4+
ms.date: 04/30/2025
5+
uid: database/azure-sql-integration
6+
---
7+
8+
# .NET Aspire Azure SQL integration
9+
10+
[!INCLUDE [includes-hosting-and-client](../includes/includes-hosting-and-client.md)]
11+
12+
[Azure SQL](https://azure.microsoft.com/products/azure-sql) is a family of relational database management systems that run in the Azure cloud. The database systems are Platform-as-a-Service (PaaS) products that enable database administrators to implement highly scalable and available databases without maintaining complex infrastructures themselves. The .NET Aspire Azure SQL Server Hosting integration provides methods to create a new Azure Database server and databases from code in your .NET Aspire app host project. In a consuming project, you can use the .NET Aspire SQL Server client integration as you would for any other SQL Server instance.
13+
14+
## Hosting integration
15+
16+
[!INCLUDE [azure-sql-hosting](includes/azure-sql-hosting.md)]
17+
18+
## Client integration
19+
20+
[!INCLUDE [sql-server-client](includes/sql-server-client.md)]
21+
22+
## See also
23+
24+
- [Azure SQL Database](/azure/azure-sql/database)
25+
- [.NET Aspire database containers sample](/samples/dotnet/aspire-samples/aspire-database-containers/)
26+
- [.NET Aspire integrations](../fundamentals/integrations-overview.md)
27+
- [.NET Aspire GitHub repo](https://github.com/dotnet/aspire)
28+
- [Azure SQL & SQL Server Aspire Samples](https://github.com/Azure-Samples/azure-sql-db-aspire)
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
---
2+
ms.topic: include
3+
---
4+
5+
The Azure SQL hosting integration models the Azure SQL server as the <xref:Aspire.Hosting.Azure.AzureSqlServerResource> type and the database as the <xref:Aspire.Hosting.Azure.AzureSqlDatabaseResource> type. To access these types and APIs, add the [📦 Aspire.Hosting.Azure.Sql](https://www.nuget.org/packages/Aspire.Hosting.Azure.Sql) NuGet package in the [app host](xref:dotnet/aspire/app-host) project.
6+
7+
### [.NET CLI](#tab/dotnet-cli)
8+
9+
```dotnetcli
10+
dotnet add package Aspire.Hosting.Azure.Sql
11+
```
12+
13+
### [PackageReference](#tab/package-reference)
14+
15+
```xml
16+
<PackageReference Include="Aspire.Hosting.Azure.Sql"
17+
Version="*" />
18+
```
19+
20+
---
21+
22+
For more information, see [dotnet add package](/dotnet/core/tools/dotnet-add-package) or [Manage package dependencies in .NET applications](/dotnet/core/tools/dependencies).
23+
24+
The Azure SQL hosting integration takes a dependency on the [📦 Aspire.Hosting.SqlServer](https://www.nuget.org/packages/Aspire.Hosting.SqlServer/) NuGet package, extending it to support Azure. Everything that you can do with the [.NET Aspire SQL Server integration](../sql-server-integration.md) and [.NET Aspire SQL Server Entity Framework Core integration](../sql-server-entity-framework-integration.md) you can also do with this integration.
25+
26+
### Add Azure SQL server resource and database resource
27+
28+
In your app host project, call <xref:Aspire.Hosting.AzureSqlExtensions.AddAzureSqlServer*> to add and return an Azure SQL server resource builder. Chain a call to the returned resource builder to <xref:Aspire.Hosting.AzureSqlExtensions.AddDatabase*>, to add an Azure SQL database resource:
29+
30+
```csharp
31+
var azureSql = builder.AddAzureSqlServer("azuresql")
32+
.AddDatabase("database");
33+
34+
var myService = builder.AddProject<Projects.MyService>()
35+
.WithReference(azureSql);
36+
```
37+
38+
The preceding call to `AddAzureSqlServer` configures the Azure SQL server resource to be deployed as an [Azure SQL Database server](/azure/azure-sql/database/sql-database-paas-overview).
39+
40+
> [!IMPORTANT]
41+
> By default, `AddAzureSqlServer` configures [Microsoft Entra ID](/azure/azure-sql/database/authentication-aad-overview) authentication. This requires changes to applications that need to connect to these resources. For more information, see [Client integration](#client-integration).
42+
43+
> [!TIP]
44+
> When you call <xref:Aspire.Hosting.AzureSqlExtensions.AddAzureSqlServer*>, it implicitly calls <xref:Aspire.Hosting.AzureProvisionerExtensions.AddAzureProvisioning*> — which adds support for generating Azure resources dynamically during app startup. The app must configure the appropriate subscription and location. For more information, see [Local provisioning: Configuration](../../azure/local-provisioning.md#configuration).
45+
46+
### Connect to an existing Azure SQL server
47+
48+
You might have an existing Azure SQL database that you want to connect to. Instead of representing a new Azure SQL server resource, you can add a connection string to the app host. To add a connection to an existing Azure SQL server, call the <xref:Aspire.Hosting.ParameterResourceBuilderExtensions.AddConnectionString*> method:
49+
50+
```csharp
51+
var builder = DistributedApplication.CreateBuilder(args);
52+
53+
var azureSql = builder.AddConnectionString("database");
54+
55+
builder.AddProject<Projects.WebApplication>("web")
56+
.WithReference(azureSql);
57+
58+
// After adding all resources, run the app...
59+
```
60+
61+
[!INCLUDE [connection-strings-alert](../../includes/connection-strings-alert.md)]
62+
63+
The connection string is configured in the app host's configuration, typically under [User Secrets](/aspnet/core/security/app-secrets), under the `ConnectionStrings` section. The app host injects this connection string as an environment variable into all dependent resources, for example:
64+
65+
```json
66+
{
67+
"ConnectionStrings": {
68+
"database": "Server=tcp:<Azure-SQL-server-name>.database.windows.net,1433;Initial Catalog=<database-name>;Encrypt=True;TrustServerCertificate=False;Connection Timeout=30;User ID=<username>;"
69+
}
70+
}
71+
```
72+
73+
The dependent resource can access the injected connection string by calling the <xref:Microsoft.Extensions.Configuration.ConfigurationExtensions.GetConnectionString*> method, and passing the connection name as the parameter, in this case `"database"`. The `GetConnectionString` API is shorthand for `IConfiguration.GetSection("ConnectionStrings")[name]`.
74+
75+
### Run Azure SQL server resource as a container
76+
77+
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.
78+
79+
To run the Azure SQL server as a container, call the <xref:Aspire.Hosting.AzureSqlExtensions.RunAsContainer*> method:
80+
81+
```csharp
82+
var builder = DistributedApplication.CreateBuilder(args);
83+
84+
var azureSql = builder.AddAzureSqlServer("azuresql")
85+
.RunAsContainer();
86+
87+
var azureSqlData = postgres.AddDatabase("database");
88+
89+
var exampleProject = builder.AddProject<Projects.ExampleProject>()
90+
.WithReference(azureSqlData);
91+
```
92+
93+
The preceding code configures an Azure SQL Database resource to run locally in a container.
94+
95+
> [!TIP]
96+
> The `RunAsContainer` method is useful for local development and testing. The API exposes an optional delegate that enables you to customize the underlying <xref:Aspire.Hosting.ApplicationModel.SqlServerServerResource> configuration. For example, you can add a data volume or data bind mount. For more information, see the [.NET Aspire SQL Server hosting integration](../sql-server-integration.md#add-sql-server-resource-with-data-volume) section.
Lines changed: 166 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,166 @@
1+
---
2+
ms.topic: include
3+
---
4+
5+
To get started with the .NET Aspire SQL Server client integration, install the [📦 Aspire.Microsoft.Data.SqlClient](https://www.nuget.org/packages/Aspire.Microsoft.Data.SqlClient) NuGet package in the client-consuming project, that is, the project for the application that uses the SQL Server client. The SQL Server client integration registers a <xref:System.Data.SqlClient.SqlConnection> instance that you can use to interact with SQL Server.
6+
7+
### [.NET CLI](#tab/dotnet-cli)
8+
9+
```dotnetcli
10+
dotnet add package Aspire.Microsoft.Data.SqlClient
11+
```
12+
13+
### [PackageReference](#tab/package-reference)
14+
15+
```xml
16+
<PackageReference Include="Aspire.Microsoft.Data.SqlClient"
17+
Version="*" />
18+
```
19+
20+
---
21+
22+
### Add SQL Server client
23+
24+
In the _:::no-loc text="Program.cs":::_ file of your client-consuming project, call the <xref:Microsoft.Extensions.Hosting.AspireSqlServerSqlClientExtensions.AddSqlServerClient*> extension method on any <xref:Microsoft.Extensions.Hosting.IHostApplicationBuilder> to register a `SqlConnection` for use via the dependency injection container. The method takes a connection name parameter.
25+
26+
```csharp
27+
builder.AddSqlServerClient(connectionName: "database");
28+
```
29+
30+
> [!TIP]
31+
> The `connectionName` parameter must match the name used when adding the SQL Server database resource in the app host project. In other words, when you call `AddDatabase` and provide a name of `database` that same name should be used when calling `AddSqlServerClient`. For more information, see [Add SQL Server resource and database resource](../sql-server-integration.md#add-sql-server-resource-and-database-resource).
32+
33+
You can then retrieve the <xref:Microsoft.Data.SqlClient.SqlConnection> instance using dependency injection. For example, to retrieve the connection from an example service:
34+
35+
```csharp
36+
public class ExampleService(SqlConnection connection)
37+
{
38+
// Use connection...
39+
}
40+
```
41+
42+
For more information on dependency injection, see [.NET dependency injection](/dotnet/core/extensions/dependency-injection).
43+
44+
### Add keyed SQL Server client
45+
46+
There might be situations where you want to register multiple `SqlConnection` instances with different connection names. To register keyed SQL Server clients, call the <xref:Microsoft.Extensions.Hosting.AspireSqlServerSqlClientExtensions.AddKeyedSqlServerClient*> method:
47+
48+
```csharp
49+
builder.AddKeyedSqlServerClient(name: "mainDb");
50+
builder.AddKeyedSqlServerClient(name: "loggingDb");
51+
```
52+
53+
> [!IMPORTANT]
54+
> When using keyed services, it's expected that your SQL Server resource configured two named databases, one for the `mainDb` and one for the `loggingDb`.
55+
56+
Then you can retrieve the `SqlConnection` instances using dependency injection. For example, to retrieve the connection from an example service:
57+
58+
```csharp
59+
public class ExampleService(
60+
[FromKeyedServices("mainDb")] SqlConnection mainDbConnection,
61+
[FromKeyedServices("loggingDb")] SqlConnection loggingDbConnection)
62+
{
63+
// Use connections...
64+
}
65+
```
66+
67+
For more information on keyed services, see [.NET dependency injection: Keyed services](/dotnet/core/extensions/dependency-injection#keyed-services).
68+
69+
### Configuration
70+
71+
The .NET Aspire SQL Server integration provides multiple options to configure the connection based on the requirements and conventions of your project.
72+
73+
#### Use a connection string
74+
75+
When using a connection string from the `ConnectionStrings` configuration section, you can provide the name of the connection string when calling the <xref:Microsoft.Extensions.Hosting.AspireSqlServerSqlClientExtensions.AddSqlServerClient*> method:
76+
77+
```csharp
78+
builder.AddSqlServerClient(connectionName: "sql");
79+
```
80+
81+
Then the connection string is retrieved from the `ConnectionStrings` configuration section:
82+
83+
```json
84+
{
85+
"ConnectionStrings": {
86+
"database": "Data Source=myserver;Initial Catalog=master"
87+
}
88+
}
89+
```
90+
91+
For more information on how to format this connection string, see the [ConnectionString](/dotnet/api/system.data.sqlclient.sqlconnection.connectionstring#remarks).
92+
93+
#### Use configuration providers
94+
95+
The .NET Aspire SQL Server integration supports <xref:Microsoft.Extensions.Configuration>. It loads the <xref:Aspire.Microsoft.Data.SqlClient.MicrosoftDataSqlClientSettings> from configuration by using the `Aspire:Microsoft:Data:SqlClient` key. The following snippet is an example of a _:::no-loc text="appsettings.json":::_ file that configures some of the options:
96+
97+
```json
98+
{
99+
"Aspire": {
100+
"Microsoft": {
101+
"Data": {
102+
"SqlClient": {
103+
"ConnectionString": "YOUR_CONNECTIONSTRING",
104+
"DisableHealthChecks": false,
105+
"DisableMetrics": true
106+
}
107+
}
108+
}
109+
}
110+
}
111+
```
112+
113+
For the complete SQL Server client integration JSON schema, see [Aspire.Microsoft.Data.SqlClient/ConfigurationSchema.json](https://github.com/dotnet/aspire/blob/v8.2.2/src/Components/Aspire.Microsoft.Data.SqlClient/ConfigurationSchema.json).
114+
115+
#### Use inline delegates
116+
117+
Also you can pass the `Action<MicrosoftDataSqlClientSettings> configureSettings` delegate to set up some or all the options inline, for example to disable health checks from code:
118+
119+
```csharp
120+
builder.AddSqlServerClient(
121+
"database",
122+
static settings => settings.DisableHealthChecks = true);
123+
```
124+
125+
### Client integration health checks
126+
127+
By default, .NET Aspire integrations enable [health checks](../../fundamentals/health-checks.md) for all services. For more information, see [.NET Aspire integrations overview](../../fundamentals/integrations-overview.md).
128+
129+
The .NET Aspire SQL Server integration:
130+
131+
- Adds the health check when <xref:Aspire.Microsoft.Data.SqlClient.MicrosoftDataSqlClientSettings.DisableHealthChecks?displayProperty=nameWithType> is `false`, which attempts to connect to the SQL Server.
132+
- Integrates with the `/health` HTTP endpoint, which specifies all registered health checks must pass for app to be considered ready to accept traffic.
133+
134+
[!INCLUDE [integration-observability-and-telemetry](../../includes/integration-observability-and-telemetry.md)]
135+
136+
#### Logging
137+
138+
The .NET Aspire SQL Server integration currently doesn't enable logging by default due to limitations of the <xref:Microsoft.Data.SqlClient>.
139+
140+
#### Tracing
141+
142+
The .NET Aspire SQL Server integration emits the following tracing activities using OpenTelemetry:
143+
144+
- `OpenTelemetry.Instrumentation.SqlClient`
145+
146+
#### Metrics
147+
148+
The .NET Aspire SQL Server integration will emit the following metrics using OpenTelemetry:
149+
150+
- Microsoft.Data.SqlClient.EventSource
151+
- `active-hard-connections`
152+
- `hard-connects`
153+
- `hard-disconnects`
154+
- `active-soft-connects`
155+
- `soft-connects`
156+
- `soft-disconnects`
157+
- `number-of-non-pooled-connections`
158+
- `number-of-pooled-connections`
159+
- `number-of-active-connection-pool-groups`
160+
- `number-of-inactive-connection-pool-groups`
161+
- `number-of-active-connection-pools`
162+
- `number-of-inactive-connection-pools`
163+
- `number-of-active-connections`
164+
- `number-of-free-connections`
165+
- `number-of-stasis-connections`
166+
- `number-of-reclaimed-connections`

0 commit comments

Comments
 (0)