{"record":{"id":"c331e976d4642bf3","repo":"microsoft/aspire","slug":"could-not-find-a-nameof-azuresqldatabaseresource-with-name","errorCode":null,"errorMessage":"Could not find a {nameof(AzureSqlDatabaseResource)} with name {database.Key}.","messagePattern":"Could not find a (.+?) with name (.+?)\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Aspire.Hosting.Azure.Sql/AzureSqlExtensions.cs","lineNumber":206,"sourceCode":"        }\n\n        var azureResource = builder.Resource;\n        var azureDatabases = builder.ApplicationBuilder.Resources\n            .OfType<AzureSqlDatabaseResource>()\n            .Where(db => db.Parent == azureResource)\n            .ToDictionary(db => db.Name);\n\n        RemoveAzureResources(builder.ApplicationBuilder, azureResource, azureDatabases);\n\n        var sqlContainer = builder.ApplicationBuilder.AddSqlServer(azureResource.Name);\n\n        azureResource.SetInnerResource(sqlContainer.Resource);\n\n        foreach (var database in azureResource.AzureSqlDatabases)\n        {\n            if (!azureDatabases.TryGetValue(database.Key, out var existingDb))\n            {\n                throw new InvalidOperationException($\"Could not find a {nameof(AzureSqlDatabaseResource)} with name {database.Key}.\");\n            }\n\n            var innerDb = sqlContainer.AddDatabase(database.Key, database.Value.DatabaseName);\n            existingDb.SetInnerResource(innerDb.Resource);\n        }\n\n        configureContainer?.Invoke(sqlContainer);\n\n        return builder;\n    }\n\n    private static void RemoveAzureResources(IDistributedApplicationBuilder appBuilder, AzureSqlServerResource azureResource, Dictionary<string, AzureSqlDatabaseResource> azureDatabases)\n    {\n        appBuilder.Resources.Remove(azureResource);\n        foreach (var database in azureDatabases)\n        {\n            appBuilder.Resources.Remove(database.Value);\n        }","sourceCodeStart":188,"sourceCodeEnd":224,"githubUrl":"https://github.com/microsoft/aspire/blob/25830f84bd145686607ad00c057b3f84e2e51d43/src/Aspire.Hosting.Azure.Sql/AzureSqlExtensions.cs#L188-L224","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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."],"exampleFix":"// before\nvar sql = builder.AddAzureSql(\"sql\").RunAsContainer();\nsql.AddDatabase(\"appdb\"); // added after switch — lookup fails\n\n// after\nvar sql = builder.AddAzureSql(\"sql\");\nsql.AddDatabase(\"appdb\");\nsql.RunAsContainer();","handlingStrategy":"validation","validationCode":"// Ensure all databases are declared before switching to container mode:\nvar sql = builder.AddAzureSql(\"sql\");\nsql.AddDatabase(\"appdb\");\nsql.RunAsContainer();","typeGuard":null,"tryCatchPattern":"try { sql.RunAsContainer(); }\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"Could not find a AzureSqlDatabaseResource\")) { /* restructure: add all databases first */ }","preventionTips":["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."],"tags":["invalid-state","azure-sql","emulator","database"],"backgroundTag":"entity-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"}