# AzureSqlExtensions Methods

- Package: [Aspire.Hosting.Azure.Sql](/reference/api/csharp/aspire.hosting.azure.sql.md)
- Type: [AzureSqlExtensions](/reference/api/csharp/aspire.hosting.azure.sql/azuresqlextensions.md)
- Kind: `Methods`
- Members: `8`

Provides extension methods for adding the Azure SQL resources to the application model.

## AddAzureSqlServer(IDistributedApplicationBuilder, string)

- Name: `AddAzureSqlServer(IDistributedApplicationBuilder, string)`
- Modifiers: `extension`
- Returns: `IResourceBuilder<AzureSqlServerResource>`
- Source: [GitHub](https://github.com/microsoft/aspire/blob/cbc352350f1a9bafbaff10d14a2c8de4ac186a48/src/Aspire.Hosting.Azure.Sql/AzureSqlExtensions.cs#L83-L99)

Adds an Azure SQL Database (server) resource to the application model.

```csharp
public static class AzureSqlExtensions
{
    public static IResourceBuilder<AzureSqlServerResource> AddAzureSqlServer(
        this IDistributedApplicationBuilder builder,
        string name)
    {
        // ...
    }
}
```

## Parameters

- `builder` (`IDistributedApplicationBuilder`)
  The builder for the distributed application.
- `name` (`string`)
  The name of the resource.

## Returns

`IResourceBuilder<AzureSqlServerResource>` -- A reference to the `ApplicationModel.IResourceBuilder`1` builder.

## ATS metadata

### ATS export

- Available to Polyglot AppHosts through the Aspire Type System.

## AddDatabase(IResourceBuilder<AzureSqlServerResource>, string, string?)

- Name: `AddDatabase(IResourceBuilder<AzureSqlServerResource>, string, string?)`
- Modifiers: `extension`
- Returns: `IResourceBuilder<AzureSqlDatabaseResource>`
- Source: [GitHub](https://github.com/microsoft/aspire/blob/cbc352350f1a9bafbaff10d14a2c8de4ac186a48/src/Aspire.Hosting.Azure.Sql/AzureSqlExtensions.cs#L114-L137)

Adds an Azure SQL Database to the application model. The Free Offer option will be used when deploying the resource in Azure

```csharp
public static class AzureSqlExtensions
{
    public static IResourceBuilder<AzureSqlDatabaseResource> AddDatabase(
        this IResourceBuilder<AzureSqlServerResource> builder,
        string name,
        string? databaseName = null)
    {
        // ...
    }
}
```

## Parameters

- `builder` (`IResourceBuilder<AzureSqlServerResource>`)
  The builder for the Azure SQL resource.
- `name` (`string`)
  The name of the resource. This name will be used as the connection string name when referenced in a dependency.
- `databaseName` (`string?`) `optional`
  The name of the database. If not provided, this defaults to the same value as `name`.

## Returns

`IResourceBuilder<AzureSqlDatabaseResource>` -- A reference to the `ApplicationModel.IResourceBuilder`1`.

## ATS metadata

### ATS export

- Available to Polyglot AppHosts through the Aspire Type System.

## AsAzureSqlDatabase(IResourceBuilder<SqlServerServerResource>)

> **Obsolete:** This method is obsolete and will be removed in a future version. Use AddAzureSqlServer instead to add an Azure SQL server resource.

- Name: `AsAzureSqlDatabase(IResourceBuilder<SqlServerServerResource>)`
- Modifiers: `extension`
- Returns: `IResourceBuilder<SqlServerServerResource>`
- Source: [GitHub](https://github.com/microsoft/aspire/blob/cbc352350f1a9bafbaff10d14a2c8de4ac186a48/src/Aspire.Hosting.Azure.Sql/AzureSqlExtensions.cs#L71)

Configures SQL Server resource to be deployed as Azure SQL Database (server).

```csharp
public static class AzureSqlExtensions
{
    public static IResourceBuilder<SqlServerServerResource> AsAzureSqlDatabase(
        this IResourceBuilder<SqlServerServerResource> builder)
    {
        // ...
    }
}
```

## Parameters

- `builder` (`IResourceBuilder<SqlServerServerResource>`)
  The builder for the SQL Server resource.

## Returns

`IResourceBuilder<SqlServerServerResource>` -- A reference to the `ApplicationModel.IResourceBuilder`1`.

## PublishAsAzureSqlDatabase(IResourceBuilder<SqlServerServerResource>)

> **Obsolete:** This method is obsolete and will be removed in a future version. Use AddAzureSqlServer instead to add an Azure SQL server resource.

- Name: `PublishAsAzureSqlDatabase(IResourceBuilder<SqlServerServerResource>)`
- Modifiers: `extension`
- Returns: `IResourceBuilder<SqlServerServerResource>`
- Source: [GitHub](https://github.com/microsoft/aspire/blob/cbc352350f1a9bafbaff10d14a2c8de4ac186a48/src/Aspire.Hosting.Azure.Sql/AzureSqlExtensions.cs#L62)

Configures SQL Server resource to be deployed as Azure SQL Database (server).

```csharp
public static class AzureSqlExtensions
{
    public static IResourceBuilder<SqlServerServerResource> PublishAsAzureSqlDatabase(
        this IResourceBuilder<SqlServerServerResource> builder)
    {
        // ...
    }
}
```

## Parameters

- `builder` (`IResourceBuilder<SqlServerServerResource>`)
  The builder for the SQL Server resource.

## Returns

`IResourceBuilder<SqlServerServerResource>` -- A reference to the `ApplicationModel.IResourceBuilder`1`.

## RunAsContainer(IResourceBuilder<AzureSqlServerResource>, Action<IResourceBuilder<SqlServerServerResource>>)

- Name: `RunAsContainer(IResourceBuilder<AzureSqlServerResource>, Action<IResourceBuilder<SqlServerServerResource>>)`
- Modifiers: `extension`
- Returns: `IResourceBuilder<AzureSqlServerResource>`
- Source: [GitHub](https://github.com/microsoft/aspire/blob/cbc352350f1a9bafbaff10d14a2c8de4ac186a48/src/Aspire.Hosting.Azure.Sql/AzureSqlExtensions.cs#L183-L215)

Configures an Azure SQL Database (server) resource to run locally in a container.

```csharp
public static class AzureSqlExtensions
{
    public static IResourceBuilder<AzureSqlServerResource> RunAsContainer(
        this IResourceBuilder<AzureSqlServerResource> builder,
        Action<IResourceBuilder<SqlServerServerResource>>? configureContainer = null)
    {
        // ...
    }
}
```

## Parameters

- `builder` (`IResourceBuilder<AzureSqlServerResource>`)
  The builder for the Azure SQL resource.
- `configureContainer` (`Action<IResourceBuilder<SqlServerServerResource>>`) `optional`
  Callback that exposes underlying container to allow for customization.

## Returns

`IResourceBuilder<AzureSqlServerResource>` -- A reference to the `ApplicationModel.IResourceBuilder`1` builder.

## Remarks

The following example creates an Azure SQL Database (server) resource that runs locally in a SQL Server container and referencing that resource in a .NET project.

```csharp
var builder = DistributedApplication.CreateBuilder(args);

var data = builder.AddAzureSqlServer("data")
    .RunAsContainer();

builder.AddProject<Projects.ProductService>()
    .WithReference(data);

builder.Build().Run();
```

## ATS metadata

### ATS export

- Available to Polyglot AppHosts through the Aspire Type System.

## WithAdminDeploymentScriptStorage(IResourceBuilder<AzureSqlServerResource>, IResourceBuilder<AzureStorageResource>)

> **Experimental:** ASPIREAZURE003 - [Learn more](/diagnostics/aspireazure003/)

- Name: `WithAdminDeploymentScriptStorage(IResourceBuilder<AzureSqlServerResource>, IResourceBuilder<AzureStorageResource>)`
- Modifiers: `extension`
- Returns: `IResourceBuilder<AzureSqlServerResource>`
- Source: [GitHub](https://github.com/microsoft/aspire/blob/cbc352350f1a9bafbaff10d14a2c8de4ac186a48/src/Aspire.Hosting.Azure.Sql/AzureSqlExtensions.cs#L451-L475)

Configures the Azure SQL Server to use the specified storage account for deployment script execution.

```csharp
public static class AzureSqlExtensions
{
    public static IResourceBuilder<AzureSqlServerResource> WithAdminDeploymentScriptStorage(
        this IResourceBuilder<AzureSqlServerResource> builder,
        IResourceBuilder<AzureStorageResource> storage)
    {
        // ...
    }
}
```

## Parameters

- `builder` (`IResourceBuilder<AzureSqlServerResource>`)
  The Azure SQL Server resource builder.
- `storage` (`IResourceBuilder<AzureStorageResource>`)
  The storage account to use for deployment scripts.

## Returns

`IResourceBuilder<AzureSqlServerResource>` -- A reference to the `ApplicationModel.IResourceBuilder`1` for chaining.

## Remarks

When an Azure SQL Server has a private endpoint, deployment scripts require a storage account to upload scripts and write logs. This method allows you to provide an explicit storage account instead of having one auto-created.

The storage account must have `AllowSharedKeyAccess` enabled, as deployment scripts need to mount file shares. If the storage is not an existing resource, this method will automatically configure `AllowSharedKeyAccess = true`.

## Examples

Provide a custom storage account for the deployment script:

```csharp
var vnet = builder.AddAzureVirtualNetwork("vnet");
var peSubnet = vnet.AddSubnet("pe-subnet", "10.0.2.0/24");

var storage = builder.AddAzureStorage("scriptstorage");
var sql = builder.AddAzureSqlServer("sql")
    .WithAdminDeploymentScriptStorage(storage);
peSubnet.AddPrivateEndpoint(sql);
```

## ATS metadata

### ATS export

- Available to Polyglot AppHosts through the Aspire Type System.

## WithAdminDeploymentScriptSubnet(IResourceBuilder<AzureSqlServerResource>, IResourceBuilder<AzureSubnetResource>)

> **Experimental:** ASPIREAZURE003 - [Learn more](/diagnostics/aspireazure003/)

- Name: `WithAdminDeploymentScriptSubnet(IResourceBuilder<AzureSqlServerResource>, IResourceBuilder<AzureSubnetResource>)`
- Modifiers: `extension`
- Returns: `IResourceBuilder<AzureSqlServerResource>`
- Source: [GitHub](https://github.com/microsoft/aspire/blob/cbc352350f1a9bafbaff10d14a2c8de4ac186a48/src/Aspire.Hosting.Azure.Sql/AzureSqlExtensions.cs#L401-L411)

Configures the Azure SQL Server to use the specified subnet for deployment script execution.

```csharp
public static class AzureSqlExtensions
{
    public static IResourceBuilder<AzureSqlServerResource> WithAdminDeploymentScriptSubnet(
        this IResourceBuilder<AzureSqlServerResource> builder,
        IResourceBuilder<AzureSubnetResource> subnet)
    {
        // ...
    }
}
```

## Parameters

- `builder` (`IResourceBuilder<AzureSqlServerResource>`)
  The Azure SQL Server resource builder.
- `subnet` (`IResourceBuilder<AzureSubnetResource>`)
  The subnet to delegate for Azure Container Instances used by deployment scripts.

## Returns

`IResourceBuilder<AzureSqlServerResource>` -- A reference to the `ApplicationModel.IResourceBuilder`1` for chaining.

## Remarks

When an Azure SQL Server has a private endpoint, deployment scripts that add database role assignments run inside Azure Container Instances (ACI). This method allows you to provide an explicit subnet for those containers instead of having one auto-created.

The specified subnet will be automatically delegated to `Microsoft.ContainerInstance/containerGroups`. Ensure the subnet has outbound network security rules allowing access to Azure Active Directory (port 443) and SQL (port 443) service tags.

## Examples

Provide a custom ACI subnet for the deployment script:

```csharp
var vnet = builder.AddAzureVirtualNetwork("vnet");
var peSubnet = vnet.AddSubnet("pe-subnet", "10.0.2.0/24");
var aciSubnet = vnet.AddSubnet("aci-subnet", "10.0.3.0/29");

var sql = builder.AddAzureSqlServer("sql")
    .WithAdminDeploymentScriptSubnet(aciSubnet);
peSubnet.AddPrivateEndpoint(sql);
```

## ATS metadata

### ATS export

- Available to Polyglot AppHosts through the Aspire Type System.

## WithDefaultAzureSku(IResourceBuilder<AzureSqlDatabaseResource>)

- Name: `WithDefaultAzureSku(IResourceBuilder<AzureSqlDatabaseResource>)`
- Modifiers: `extension`
- Returns: `IResourceBuilder<AzureSqlDatabaseResource>`
- Source: [GitHub](https://github.com/microsoft/aspire/blob/cbc352350f1a9bafbaff10d14a2c8de4ac186a48/src/Aspire.Hosting.Azure.Sql/AzureSqlExtensions.cs#L151-L152)

Configures the Azure SQL Database to be deployed use the default SKU provided by Azure. Please be aware that the Azure default Sku might not take advantage of the free offer.

```csharp
public static class AzureSqlExtensions
{
    public static IResourceBuilder<AzureSqlDatabaseResource> WithDefaultAzureSku(
        this IResourceBuilder<AzureSqlDatabaseResource> builder)
    {
        // ...
    }
}
```

## Parameters

- `builder` (`IResourceBuilder<AzureSqlDatabaseResource>`)
  The builder for the Azure SQL resource.

## Returns

`IResourceBuilder<AzureSqlDatabaseResource>` -- A reference to the `ApplicationModel.IResourceBuilder`1`.

## ATS metadata

### ATS export

- Available to Polyglot AppHosts through the Aspire Type System.
