{"record":{"id":"884320454b00bbbb","repo":"microsoft/aspire","slug":"could-not-find-a-nameof","errorCode":null,"errorMessage":"Could not find a {nameof(AzurePostgresFlexibleServerDatabaseResource)} with name {database.Key}.","messagePattern":"Could not find a (.+?) with name (.+?)\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Aspire.Hosting.Azure.PostgreSQL/AzurePostgresExtensions.cs","lineNumber":255,"sourceCode":"        var userNameParameterBuilder = azureResource.UserNameParameter is not null ?\n            builder.ApplicationBuilder.CreateResourceBuilder(azureResource.UserNameParameter) :\n            null;\n        var passwordParameterBuilder = azureResource.PasswordParameter is not null ?\n            builder.ApplicationBuilder.CreateResourceBuilder(azureResource.PasswordParameter) :\n            null;\n\n        var postgresContainer = builder.ApplicationBuilder.AddPostgres(\n            azureResource.Name,\n            userNameParameterBuilder,\n            passwordParameterBuilder);\n\n        azureResource.SetInnerResource(postgresContainer.Resource);\n\n        foreach (var database in azureResource.Databases)\n        {\n            if (!azureDatabases.TryGetValue(database.Key, out var existingDb))\n            {\n                throw new InvalidOperationException($\"Could not find a {nameof(AzurePostgresFlexibleServerDatabaseResource)} with name {database.Key}.\");\n            }\n\n            var innerDb = postgresContainer.AddDatabase(database.Key, database.Value);\n            existingDb.SetInnerResource(innerDb.Resource);\n        }\n\n        configureContainer?.Invoke(postgresContainer);\n\n        return builder;\n    }\n\n    private static void RemoveAzureResources(IDistributedApplicationBuilder appBuilder, AzurePostgresFlexibleServerResource azureResource, Dictionary<string, AzurePostgresFlexibleServerDatabaseResource> azureDatabases)\n    {\n        appBuilder.Resources.Remove(azureResource);\n        foreach (var database in azureDatabases)\n        {\n            appBuilder.Resources.Remove(database.Value);\n        }","sourceCodeStart":237,"sourceCodeEnd":273,"githubUrl":"https://github.com/microsoft/aspire/blob/25830f84bd145686607ad00c057b3f84e2e51d43/src/Aspire.Hosting.Azure.PostgreSQL/AzurePostgresExtensions.cs#L237-L273","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Ensure every database added to the Azure resource (via AddDatabase) is also present in the azureDatabases dictionary passed to RunAsContainer.","Match keys exactly — the lookup is by the dictionary key, so fix casing/spacing mismatches.","Reorder code so all Azure-side AddDatabase calls happen before RunAsContainer synchronization.","If upgrading, check the release notes: newer versions may handle this mapping automatically."],"exampleFix":"// before\nvar db = postgres.AddDatabase(\"appdb\");\npostgres.RunAsContainer(cfg => { /* 'appdb' missing from azureDatabases */ });\n\n// after\nvar db = postgres.AddDatabase(\"appdb\");\npostgres.RunAsContainer(); // ensure the mapping includes 'appdb' before switching","handlingStrategy":"validation","validationCode":"foreach (var db in azureResource.Databases.Keys)\n{\n    if (!azureDatabases.ContainsKey(db)) throw new InvalidOperationException($\"Database '{db}' must be registered before RunAsContainer\");\n}","typeGuard":null,"tryCatchPattern":"try { postgres.RunAsContainer(); }\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"AzurePostgresFlexibleServerDatabaseResource\")) { /* add the missing database or fix the key spelling */ }","preventionTips":["Call AddDatabase on the Azure resource before switching to RunAsContainer.","Keep database keys consistent (exact casing and spelling) between Azure and container wiring.","After renames, grep for the old database key across the AppHost."],"tags":["azure","postgresql","resource-mapping","run-as-container"],"backgroundTag":"record-not-found","analyzedSha":"25830f84bd145686607ad00c057b3f84e2e51d43","analyzedAt":"2026-09-16T11:10:06.193Z","contentChangedAt":"2026-09-16T11:10:06.193Z","schemaVersion":2},"datasetVersion":"2026-09-21T04:17:39.646Z"}