# SqlServerBuilderExtensions Methods

- Package: [Aspire.Hosting.SqlServer](/reference/api/csharp/aspire.hosting.sqlserver.md)
- Type: [SqlServerBuilderExtensions](/reference/api/csharp/aspire.hosting.sqlserver/sqlserverbuilderextensions.md)
- Kind: `Methods`
- Members: `7`

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

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

- Name: `AddDatabase(IResourceBuilder<SqlServerServerResource>, string, string?)`
- Modifiers: `extension`
- Returns: `IResourceBuilder<SqlServerDatabaseResource>`
- Source: [GitHub](https://github.com/microsoft/aspire/blob/cbc352350f1a9bafbaff10d14a2c8de4ac186a48/src/Aspire.Hosting.SqlServer/SqlServerBuilderExtensions.cs#L121-L147)

Adds a SQL Server database to the application model. This is a child resource of a [SqlServerServerResource](/reference/api/csharp/aspire.hosting.sqlserver/sqlserverserverresource.md).

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

## Parameters

- `builder` (`IResourceBuilder<SqlServerServerResource>`)
  The SQL Server resource builders.
- `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<SqlServerDatabaseResource>` -- A reference to the `ApplicationModel.IResourceBuilder`1`.

## Remarks

When adding a [SqlServerDatabaseResource](/reference/api/csharp/aspire.hosting.sqlserver/sqlserverdatabaseresource.md) to your application model the resource can then be referenced by other resources using the resource name. When the dependent resource is using the extension method `ResourceBuilderExtensions.WaitFor` then the dependent resource will wait until the SQL Server database is available.

Note that calling [SqlServerBuilderExtensions.AddDatabase(IResourceBuilder<SqlServerServerResource>, string, string?)](/reference/api/csharp/aspire.hosting.sqlserver/sqlserverbuilderextensions/methods.md#adddatabase-iresourcebuilder-sqlserverserverresource-string-string) will result in the database being created on the SQL Server when the server becomes ready. The database creation happens automatically as part of the resource lifecycle.

## ATS metadata

### ATS export

- Available to Polyglot AppHosts through the Aspire Type System.

## AddSqlServer(IDistributedApplicationBuilder, string, IResourceBuilder<ParameterResource>, int?)

- Name: `AddSqlServer(IDistributedApplicationBuilder, string, IResourceBuilder<ParameterResource>, int?)`
- Modifiers: `extension`
- Returns: `IResourceBuilder<SqlServerServerResource>`
- Source: [GitHub](https://github.com/microsoft/aspire/blob/cbc352350f1a9bafbaff10d14a2c8de4ac186a48/src/Aspire.Hosting.SqlServer/SqlServerBuilderExtensions.cs#L40-L92)

Adds a SQL Server resource to the application model. A container is used for local development.

```csharp
public static class SqlServerBuilderExtensions
{
    public static IResourceBuilder<SqlServerServerResource> AddSqlServer(
        this IDistributedApplicationBuilder builder,
        string name,
        IResourceBuilder<ParameterResource>? password = null,
        int? port = null)
    {
        // ...
    }
}
```

## Parameters

- `builder` (`IDistributedApplicationBuilder`)
  The `Hosting.IDistributedApplicationBuilder`.
- `name` (`string`)
  The name of the resource. This name will be used as the connection string name when referenced in a dependency.
- `password` (`IResourceBuilder<ParameterResource>`) `optional`
  The parameter used to provide the administrator password for the SQL Server resource. If `null` a random password will be generated.
- `port` (`int?`) `optional`
  The host port for the SQL Server.

## Returns

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

## Remarks

This version of the package defaults to the tag of the / container image.

## ATS metadata

### ATS export

- Available to Polyglot AppHosts through the Aspire Type System.

## WithCreationScript(IResourceBuilder<SqlServerDatabaseResource>, string)

- Name: `WithCreationScript(IResourceBuilder<SqlServerDatabaseResource>, string)`
- Modifiers: `extension`
- Returns: `IResourceBuilder<SqlServerDatabaseResource>`
- Source: [GitHub](https://github.com/microsoft/aspire/blob/cbc352350f1a9bafbaff10d14a2c8de4ac186a48/src/Aspire.Hosting.SqlServer/SqlServerBuilderExtensions.cs#L218-L223)

Defines the SQL script used to create the database.

```csharp
public static class SqlServerBuilderExtensions
{
    public static IResourceBuilder<SqlServerDatabaseResource> WithCreationScript(
        this IResourceBuilder<SqlServerDatabaseResource> builder,
        string script)
    {
        // ...
    }
}
```

## Parameters

- `builder` (`IResourceBuilder<SqlServerDatabaseResource>`)
  The builder for the [SqlServerDatabaseResource](/reference/api/csharp/aspire.hosting.sqlserver/sqlserverdatabaseresource.md).
- `script` (`string`)
  The SQL script used to create the database.

## Returns

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

## Remarks

Default script is

```csharp
IF ( NOT EXISTS ( SELECT 1 FROM sys.databases WHERE name = @DatabaseName ) ) CREATE DATABASE [<QUOTED_DATABASE_NAME%gt;];
```

## ATS metadata

### ATS export

- Available to Polyglot AppHosts through the Aspire Type System.

## WithDataBindMount(IResourceBuilder<SqlServerServerResource>, string, bool)

- Name: `WithDataBindMount(IResourceBuilder<SqlServerServerResource>, string, bool)`
- Modifiers: `extension`
- Returns: `IResourceBuilder<SqlServerServerResource>`
- Source: [GitHub](https://github.com/microsoft/aspire/blob/cbc352350f1a9bafbaff10d14a2c8de4ac186a48/src/Aspire.Hosting.SqlServer/SqlServerBuilderExtensions.cs#L181-L201)

Adds a bind mount for the data folder to a SQL Server resource.

```csharp
public static class SqlServerBuilderExtensions
{
    public static IResourceBuilder<SqlServerServerResource> WithDataBindMount(
        this IResourceBuilder<SqlServerServerResource> builder,
        string source,
        bool isReadOnly = false)
    {
        // ...
    }
}
```

## Parameters

- `builder` (`IResourceBuilder<SqlServerServerResource>`)
  The resource builder.
- `source` (`string`)
  The source directory on the host to mount into the container.
- `isReadOnly` (`bool`) `optional`
  A flag that indicates if this is a read-only mount.

## Returns

`IResourceBuilder<SqlServerServerResource>` -- The `ApplicationModel.IResourceBuilder`1`.

## Remarks

The container starts up as non-root and the `source` directory must be readable by the user that the container runs as. https://learn.microsoft.com/sql/linux/sql-server-linux-docker-container-configure?view=sql-server-ver16&pivots=cs1-bash#mount-a-host-directory-as-data-volume

## ATS metadata

### ATS export

- Available to Polyglot AppHosts through the Aspire Type System.

## WithDataVolume(IResourceBuilder<SqlServerServerResource>, string?, bool)

- Name: `WithDataVolume(IResourceBuilder<SqlServerServerResource>, string?, bool)`
- Modifiers: `extension`
- Returns: `IResourceBuilder<SqlServerServerResource>`
- Source: [GitHub](https://github.com/microsoft/aspire/blob/cbc352350f1a9bafbaff10d14a2c8de4ac186a48/src/Aspire.Hosting.SqlServer/SqlServerBuilderExtensions.cs#L161-L163)

Adds a named volume for the data folder to a SQL Server resource.

```csharp
public static class SqlServerBuilderExtensions
{
    public static IResourceBuilder<SqlServerServerResource> WithDataVolume(
        this IResourceBuilder<SqlServerServerResource> builder,
        string? name = null,
        bool isReadOnly = false)
    {
        // ...
    }
}
```

## Parameters

- `builder` (`IResourceBuilder<SqlServerServerResource>`)
  The resource builder.
- `name` (`string?`) `optional`
  The name of the volume. Defaults to an auto-generated name based on the application and resource names.
- `isReadOnly` (`bool`) `optional`
  A flag that indicates if this is a read-only volume.

## Returns

`IResourceBuilder<SqlServerServerResource>` -- The `ApplicationModel.IResourceBuilder`1`.

## ATS metadata

### ATS export

- Available to Polyglot AppHosts through the Aspire Type System.

## WithHostPort(IResourceBuilder<SqlServerServerResource>, int?)

- Name: `WithHostPort(IResourceBuilder<SqlServerServerResource>, int?)`
- Modifiers: `extension`
- Returns: `IResourceBuilder<SqlServerServerResource>`
- Source: [GitHub](https://github.com/microsoft/aspire/blob/cbc352350f1a9bafbaff10d14a2c8de4ac186a48/src/Aspire.Hosting.SqlServer/SqlServerBuilderExtensions.cs#L253-L257)

Configures the host port that the SqlServer resource is exposed on instead of using randomly assigned port.

```csharp
public static class SqlServerBuilderExtensions
{
    public static IResourceBuilder<SqlServerServerResource> WithHostPort(
        this IResourceBuilder<SqlServerServerResource> builder,
        int? port)
    {
        // ...
    }
}
```

## Parameters

- `builder` (`IResourceBuilder<SqlServerServerResource>`)
  The resource builder.
- `port` (`int?`)
  The port to bind on the host. If `null` is used random port will be assigned.

## Returns

`IResourceBuilder<SqlServerServerResource>` -- The `ApplicationModel.IResourceBuilder`1`.

## ATS metadata

### ATS export

- Available to Polyglot AppHosts through the Aspire Type System.

## WithPassword(IResourceBuilder<SqlServerServerResource>, IResourceBuilder<ParameterResource>)

- Name: `WithPassword(IResourceBuilder<SqlServerServerResource>, IResourceBuilder<ParameterResource>)`
- Modifiers: `extension`
- Returns: `IResourceBuilder<SqlServerServerResource>`
- Source: [GitHub](https://github.com/microsoft/aspire/blob/cbc352350f1a9bafbaff10d14a2c8de4ac186a48/src/Aspire.Hosting.SqlServer/SqlServerBuilderExtensions.cs#L236-L240)

Configures the password that the SqlServer resource is used.

```csharp
public static class SqlServerBuilderExtensions
{
    public static IResourceBuilder<SqlServerServerResource> WithPassword(
        this IResourceBuilder<SqlServerServerResource> builder,
        IResourceBuilder<ParameterResource> password)
    {
        // ...
    }
}
```

## Parameters

- `builder` (`IResourceBuilder<SqlServerServerResource>`)
  The resource builder.
- `password` (`IResourceBuilder<ParameterResource>`)
  The parameter used to provide the password for the SqlServer resource.

## Returns

`IResourceBuilder<SqlServerServerResource>` -- The `ApplicationModel.IResourceBuilder`1`.

## ATS metadata

### ATS export

- Available to Polyglot AppHosts through the Aspire Type System.
