liveBook · Manning

AddDatabase

As explained in Building Distributed Applications that Work

AddDatabase is a .NET Aspire hosting API method that registers or creates a database associated with a database server resource.

AddDatabase registers a named database under a database-server resource and creates the database automatically using provider-specific SQL. The name also identifies the database resource for service discovery. Unlike Aspire 8, current behavior creates the database rather than only adding a reference and connection string. section 3.3

var sql = builder.AddSqlServer("sql").WithLifetime(ContainerLifetime.Persistent);
var sqldb = sql.AddDatabase("sqldb");

var apiService = builder
    .AddProject<Projects.OnlineShop_ApiService>("apiservice")
    .WithHttpHealthCheck("/health")
    .WithReference(idp)
    .WaitFor(idp)
    .WaitFor(sqldb)
    .WithReference(sqldb);

See the SQL Server registration and how an API service waits for and references the database resource. section 3.3

How Building Distributed Applications that Work covers it

How deep it goes
Explains registration and automatic provider-specific creation, contrasting current behavior with Aspire 8.
Examples
Shows a database named sqldb registered under SQL Server and referenced by an API service.
What it assumes
Starts from an Aspire host project and SQL Server resource, and identifies the required SQL Server hosting package.
What you can do after
Configure a database resource under a SQL Server resource for an API service to wait for and reference.
How it connects
Links SQL Server hosting with database creation, connection strings, service discovery, and dependent-service references.

This explainer was generated by Manning's knowledge graph from the text of Building Distributed Applications that Work. The section links point to the parts of the book it draws on.