microsoft/aspire · error · InvalidOperationException

Could not find a with name .

Error message

Could not find a {nameof(AzurePostgresFlexibleServerDatabaseResource)} with name {database.Key}.

What it means

When an AzurePostgresFlexibleServerResource is switched to run as a local container (RunAsContainer), each database registered on the Azure resource must map to a database that already exists on the inner container resource. If azureResource.Databases contains a key absent from azureDatabases, the synchronization cannot proceed and this InvalidOperationException is thrown, since there is no AzurePostgresFlexibleServerDatabaseResource to bind to the container database.

Solutions

  1. Ensure every database added to the Azure resource (via AddDatabase) is also present in the azureDatabases dictionary passed to RunAsContainer.
  2. Match keys exactly — the lookup is by the dictionary key, so fix casing/spacing mismatches.
  3. Reorder code so all Azure-side AddDatabase calls happen before RunAsContainer synchronization.
  4. If upgrading, check the release notes: newer versions may handle this mapping automatically.

Example fix

// before
var db = postgres.AddDatabase("appdb");
postgres.RunAsContainer(cfg => { /* 'appdb' missing from azureDatabases */ });

// after
var db = postgres.AddDatabase("appdb");
postgres.RunAsContainer(); // ensure the mapping includes 'appdb' before switching
Defensive patterns

Strategy: validation

Validate before calling

foreach (var db in azureResource.Databases.Keys)
{
    if (!azureDatabases.ContainsKey(db)) throw new InvalidOperationException($"Database '{db}' must be registered before RunAsContainer");
}

Try / catch

try { postgres.RunAsContainer(); }
catch (InvalidOperationException ex) when (ex.Message.Contains("AzurePostgresFlexibleServerDatabaseResource")) { /* add the missing database or fix the key spelling */ }

Prevention

When it happens

Trigger: Calling .RunAsContainer() on an Azure Postgres resource whose Databases collection includes a database key not present in the resolved azureDatabases dictionary — e.g. a database was added on the Azure resource after the container mapping was built, or the key differs in casing/spelling.

Common situations: Local dev emulation with RunAsContainer where PublishMode adds databases but the container path misses them; renaming a database on the Azure resource; wiring AddDatabase calls in an order that leaves the dictionaries out of sync.

Understand the failure class

Background: Record Not Found Errors: "not found", RecordNotFound, and "was not found" — what they mean and how to fix them — this error's family across 28 libraries.

Related errors


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

Appendix: source

Thrown at src/Aspire.Hosting.Azure.PostgreSQL/AzurePostgresExtensions.cs:255

        var userNameParameterBuilder = azureResource.UserNameParameter is not null ?
            builder.ApplicationBuilder.CreateResourceBuilder(azureResource.UserNameParameter) :
            null;
        var passwordParameterBuilder = azureResource.PasswordParameter is not null ?
            builder.ApplicationBuilder.CreateResourceBuilder(azureResource.PasswordParameter) :
            null;

        var postgresContainer = builder.ApplicationBuilder.AddPostgres(
            azureResource.Name,
            userNameParameterBuilder,
            passwordParameterBuilder);

        azureResource.SetInnerResource(postgresContainer.Resource);

        foreach (var database in azureResource.Databases)
        {
            if (!azureDatabases.TryGetValue(database.Key, out var existingDb))
            {
                throw new InvalidOperationException($"Could not find a {nameof(AzurePostgresFlexibleServerDatabaseResource)} with name {database.Key}.");
            }

            var innerDb = postgresContainer.AddDatabase(database.Key, database.Value);
            existingDb.SetInnerResource(innerDb.Resource);
        }

        configureContainer?.Invoke(postgresContainer);

        return builder;
    }

    private static void RemoveAzureResources(IDistributedApplicationBuilder appBuilder, AzurePostgresFlexibleServerResource azureResource, Dictionary<string, AzurePostgresFlexibleServerDatabaseResource> azureDatabases)
    {
        appBuilder.Resources.Remove(azureResource);
        foreach (var database in azureDatabases)
        {
            appBuilder.Resources.Remove(database.Value);
        }

View on GitHub (pinned to 25830f84bd)