microsoft/aspire · error · InvalidOperationException
Could not find a with name .
Error message
Could not find a {nameof(AzureSqlDatabaseResource)} with name {database.Key}. What it means
RunAsContainer for Azure SQL throws InvalidOperationException when a database declared on the AzureSqlServerResource has no matching entry in the inner container's database collection. This is an internal consistency check: switching the server to a container emulator must find every database already added to the Azure resource.
Solutions
- Ensure all databases are added via sql.AddDatabase(...) before calling RunAsContainer.
- Do not manually construct/add AzureSqlDatabaseResource instances that bypass the server's AzureSqlDatabases collection.
- Update Aspire packages so all Azure SQL extension versions match.
- Inspect the database.Key named in the message and add a matching database to the builder chain.
Example fix
// before
var sql = builder.AddAzureSql("sql").RunAsContainer();
sql.AddDatabase("appdb"); // added after switch — lookup fails
// after
var sql = builder.AddAzureSql("sql");
sql.AddDatabase("appdb");
sql.RunAsContainer(); Defensive patterns
Strategy: validation
Validate before calling
// Ensure all databases are declared before switching to container mode:
var sql = builder.AddAzureSql("sql");
sql.AddDatabase("appdb");
sql.RunAsContainer(); Try / catch
try { sql.RunAsContainer(); }
catch (InvalidOperationException ex) when (ex.Message.Contains("Could not find a AzureSqlDatabaseResource")) { /* restructure: add all databases first */ } Prevention
- Declare every database with AddDatabase before RunAsContainer.
- Never add Azure SQL databases after switching to emulator mode.
- Keep all Aspire.Hosting.Azure.* packages at the same version.
- Avoid constructing AzureSqlDatabaseResource outside the builder chain.
When it happens
Trigger: Calling RunAsContainer after databases were added to the Azure SQL server resource in a way that did not register them on the inner container — e.g. adding databases after RunAsContainer, or interleaving extension versions that bypass SetInnerResource registration.
Common situations: Mixed use of direct resource construction and extension methods; adding databases to the server after switching to emulator mode; API version mismatches where the database key maps differ.
Understand the failure class
Background: "Not found" and "does not exist" errors: why "Task not found", "No such folder", and "Can't find" fire when a lookup comes back empty — this error's family across 14 libraries.
Related errors
- The Azure Storage resource is not running in the local…
- A Database could not be configured. Ensure valid connection…
- Already connected to
- ASPIRERADIUS072
- BlobServiceClient is not initialized.
AI-assisted analysis of microsoft/aspire@25830f84bd (2026-09-16).
Data as JSON: /api/errors/c331e976d4642bf3.
Report an issue: GitHub.
Appendix: source
Thrown at src/Aspire.Hosting.Azure.Sql/AzureSqlExtensions.cs:206
}
var azureResource = builder.Resource;
var azureDatabases = builder.ApplicationBuilder.Resources
.OfType<AzureSqlDatabaseResource>()
.Where(db => db.Parent == azureResource)
.ToDictionary(db => db.Name);
RemoveAzureResources(builder.ApplicationBuilder, azureResource, azureDatabases);
var sqlContainer = builder.ApplicationBuilder.AddSqlServer(azureResource.Name);
azureResource.SetInnerResource(sqlContainer.Resource);
foreach (var database in azureResource.AzureSqlDatabases)
{
if (!azureDatabases.TryGetValue(database.Key, out var existingDb))
{
throw new InvalidOperationException($"Could not find a {nameof(AzureSqlDatabaseResource)} with name {database.Key}.");
}
var innerDb = sqlContainer.AddDatabase(database.Key, database.Value.DatabaseName);
existingDb.SetInnerResource(innerDb.Resource);
}
configureContainer?.Invoke(sqlContainer);
return builder;
}
private static void RemoveAzureResources(IDistributedApplicationBuilder appBuilder, AzureSqlServerResource azureResource, Dictionary<string, AzureSqlDatabaseResource> azureDatabases)
{
appBuilder.Resources.Remove(azureResource);
foreach (var database in azureDatabases)
{
appBuilder.Resources.Remove(database.Value);
}View on GitHub (pinned to 25830f84bd)