Skip to content
Docs Try Aspire

SqlServerBuilderExtensions Methods

Class Methods 7 members
Provides extension methods for adding SQL Server resources to the application model.
AddDatabase(IResourceBuilder<SqlServerServerResource>, string, string?) Section titled AddDatabase(IResourceBuilder<SqlServerServerResource>, string, string?) extension IResourceBuilder<SqlServerDatabaseResource>
Adds a SQL Server database to the application model. This is a child resource of a SqlServerServerResource.
public static class SqlServerBuilderExtensions
{
public static IResourceBuilder<SqlServerDatabaseResource> AddDatabase(
this IResourceBuilder<SqlServerServerResource> builder,
string name,
string? databaseName = null)
{
// ...
}
}
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.
IResourceBuilder<SqlServerDatabaseResource> A reference to the ApplicationModel.IResourceBuilder`1.

When adding a SqlServerDatabaseResource 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 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.

AddSqlServer(IDistributedApplicationBuilder, string, IResourceBuilder<ParameterResource>, int?) Section titled AddSqlServer(IDistributedApplicationBuilder, string, IResourceBuilder<ParameterResource>, int?) extension IResourceBuilder<SqlServerServerResource>
Adds a SQL Server resource to the application model. A container is used for local development.
public static class SqlServerBuilderExtensions
{
public static IResourceBuilder<SqlServerServerResource> AddSqlServer(
this IDistributedApplicationBuilder builder,
string name,
IResourceBuilder<ParameterResource>? password = null,
int? port = null)
{
// ...
}
}
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.
IResourceBuilder<SqlServerServerResource> A reference to the ApplicationModel.IResourceBuilder`1.
This version of the package defaults to the tag of the / container image.
WithCreationScript(IResourceBuilder<SqlServerDatabaseResource>, string) Section titled WithCreationScript(IResourceBuilder<SqlServerDatabaseResource>, string) extension IResourceBuilder<SqlServerDatabaseResource>
Defines the SQL script used to create the database.
public static class SqlServerBuilderExtensions
{
public static IResourceBuilder<SqlServerDatabaseResource> WithCreationScript(
this IResourceBuilder<SqlServerDatabaseResource> builder,
string script)
{
// ...
}
}
builder IResourceBuilder<SqlServerDatabaseResource> The builder for the SqlServerDatabaseResource.
script string The SQL script used to create the database.
IResourceBuilder<SqlServerDatabaseResource> A reference to the ApplicationModel.IResourceBuilder`1.
Default script is
IF (
NOT EXISTS (
SELECT 1 FROM sys.databases WHERE name = @DatabaseName ) ) CREATE DATABASE [<QUOTED_DATABASE_NAME%gt;];
WithDataBindMount(IResourceBuilder<SqlServerServerResource>, string, bool) Section titled WithDataBindMount(IResourceBuilder<SqlServerServerResource>, string, bool) extension IResourceBuilder<SqlServerServerResource>
Adds a bind mount for the data folder to a SQL Server resource.
public static class SqlServerBuilderExtensions
{
public static IResourceBuilder<SqlServerServerResource> WithDataBindMount(
this IResourceBuilder<SqlServerServerResource> builder,
string source,
bool isReadOnly = false)
{
// ...
}
}
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.
IResourceBuilder<SqlServerServerResource> The ApplicationModel.IResourceBuilder`1.
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
WithDataVolume(IResourceBuilder<SqlServerServerResource>, string?, bool) Section titled WithDataVolume(IResourceBuilder<SqlServerServerResource>, string?, bool) extension IResourceBuilder<SqlServerServerResource>
Adds a named volume for the data folder to a SQL Server resource.
public static class SqlServerBuilderExtensions
{
public static IResourceBuilder<SqlServerServerResource> WithDataVolume(
this IResourceBuilder<SqlServerServerResource> builder,
string? name = null,
bool isReadOnly = false)
{
// ...
}
}
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.
IResourceBuilder<SqlServerServerResource> The ApplicationModel.IResourceBuilder`1.
WithHostPort(IResourceBuilder<SqlServerServerResource>, int?) Section titled WithHostPort(IResourceBuilder<SqlServerServerResource>, int?) extension IResourceBuilder<SqlServerServerResource>
Configures the host port that the SqlServer resource is exposed on instead of using randomly assigned port.
public static class SqlServerBuilderExtensions
{
public static IResourceBuilder<SqlServerServerResource> WithHostPort(
this IResourceBuilder<SqlServerServerResource> builder,
int? port)
{
// ...
}
}
builder IResourceBuilder<SqlServerServerResource> The resource builder.
port int? The port to bind on the host. If null is used random port will be assigned.
IResourceBuilder<SqlServerServerResource> The ApplicationModel.IResourceBuilder`1.
WithPassword(IResourceBuilder<SqlServerServerResource>, IResourceBuilder<ParameterResource>) Section titled WithPassword(IResourceBuilder<SqlServerServerResource>, IResourceBuilder<ParameterResource>) extension IResourceBuilder<SqlServerServerResource>
Configures the password that the SqlServer resource is used.
public static class SqlServerBuilderExtensions
{
public static IResourceBuilder<SqlServerServerResource> WithPassword(
this IResourceBuilder<SqlServerServerResource> builder,
IResourceBuilder<ParameterResource> password)
{
// ...
}
}
builder IResourceBuilder<SqlServerServerResource> The resource builder.
password IResourceBuilder<ParameterResource> The parameter used to provide the password for the SqlServer resource.
IResourceBuilder<SqlServerServerResource> The ApplicationModel.IResourceBuilder`1.