microsoft/aspire · error · ArgumentNullException

parent

Error message

parent

What it means

AzureSqlDatabaseResource throws ArgumentNullException when the parent AzureSqlServerResource is null. Each database must reference its server so the connection string expression can be composed from the server resource.

Solutions

  1. Pass the AzureSqlServerResource created by AddAzureSql as the parent.
  2. Use server.AddDatabase(...) instead of constructing the resource manually.
  3. Guard the server variable for null before constructing the database resource.

Example fix

// before
var db = new AzureSqlDatabaseResource("appdb", "appdb", GetServerMaybeNull());

// after
var sql = builder.AddAzureSql("sql");
var db = sql.AddDatabase("appdb");
Defensive patterns

Strategy: validation

Validate before calling

if (serverResource is null) throw new InvalidOperationException("Create the Azure SQL server before adding databases.");

Type guard

if (serverResource is not null) { var db = new AzureSqlDatabaseResource(name, dbName, serverResource); }

Prevention

When it happens

Trigger: Constructing AzureSqlDatabaseResource directly with a null parent server resource instead of via AddDatabase on a server builder.

Common situations: Manual construction in custom extensions/tests; passing the result of a failed server lookup (null) as the parent.

Related errors


AI-assisted analysis of microsoft/aspire@25830f84bd (2026-09-16). Data as JSON: /api/errors/9d88516d64473faf. Report an issue: GitHub.

Appendix: source

Thrown at src/Aspire.Hosting.Azure.Sql/AzureSqlDatabaseResource.cs:31

/// </summary>
/// <param name="name">The name of the resource.</param>
/// <param name="databaseName">The database name.</param>
/// <param name="parent">The Azure SQL Database (server) parent resource associated with this database.</param>
/// <ats-summary>A resource that represents an Azure SQL database. This is a child resource of an <ats-see cref="!:type:AzureSqlServerResource" />.</ats-summary>
[DebuggerDisplay("Type = {GetType().Name,nq}, Name = {Name}, Database = {DatabaseName}")]
[AspireExport(ExposeProperties = true)]
public class AzureSqlDatabaseResource(string name, string databaseName, AzureSqlServerResource parent)
    : Resource(name), IResourceWithParent<AzureSqlServerResource>, IResourceWithConnectionString
{
    /// <summary>
    /// SKU associated with the free offer
    /// </summary>
    internal const string FREE_DB_SKU = "GP_S_Gen5_2";

    /// <summary>
    /// Gets the parent Azure SQL Database (server) resource.
    /// </summary>
    public AzureSqlServerResource Parent { get; } = parent ?? throw new ArgumentNullException(nameof(parent));

    /// <summary>
    /// Gets the connection string expression for the Azure SQL database.
    /// </summary>
    public ReferenceExpression ConnectionStringExpression =>
        ReferenceExpression.Create($"{Parent};Database={DatabaseName}");

    /// <summary>
    /// Gets the database name.
    /// </summary>
    public string DatabaseName { get; } = ThrowIfNullOrEmpty(databaseName);

    /// <summary>  
    /// Gets or Sets the database SKU name  
    /// </summary>  
    internal bool UseDefaultAzureSku { get; set; } // Default to false  

    /// <summary>

View on GitHub (pinned to 25830f84bd)