{"record":{"id":"3b99cf6cc23b8871","repo":"microsoft/aspire","slug":"value-cannot-be-null-parameter-parent-3b99cf","errorCode":null,"errorMessage":"Value cannot be null. (Parameter 'parent')","messagePattern":"Value cannot be null\\. \\(Parameter 'parent'\\)","errorType":"validation","errorClass":"ArgumentNullException","httpStatus":null,"severity":"error","filePath":"src/Aspire.Hosting.Azure.CosmosDB/AzureCosmosDBContainerResource.cs","lineNumber":43,"sourceCode":"    /// <param name=\"containerName\">The container name.</param>\n    /// <param name=\"partitionKeyPaths\">The hierarchical partition key paths.</param>\n    /// <param name=\"parent\">The parent Azure Cosmos DB database resource.</param>\n    public AzureCosmosDBContainerResource(string name, string containerName, IEnumerable<string> partitionKeyPaths, AzureCosmosDBDatabaseResource parent) : base(name)\n    {\n        ArgumentException.ThrowIfNullOrEmpty(containerName);\n        ArgumentNullException.ThrowIfNull(partitionKeyPaths);\n        var partitionKeyPathsArray = partitionKeyPaths.ToArray();\n        if (partitionKeyPathsArray.Length == 0)\n        {\n            throw new ArgumentException(\"At least one partition key path should be provided.\", nameof(partitionKeyPaths));\n        }\n        if (partitionKeyPaths.Any(string.IsNullOrEmpty))\n        {\n            throw new ArgumentException(\"Partition key paths cannot contain null or empty strings.\", nameof(partitionKeyPaths));\n        }\n        ContainerProperties = new ContainerProperties(containerName, partitionKeyPathsArray);\n\n        Parent = parent ?? throw new ArgumentNullException(nameof(parent));\n    }\n\n    /// <summary>\n    /// Initializes a new instance of the <see cref=\"AzureCosmosDBContainerResource\"/> class.\n    /// </summary>\n    /// <param name=\"name\">The resource name.</param>\n    /// <param name=\"containerName\">The container name.</param>\n    /// <param name=\"partitionKeyPath\">The partition key path.</param>\n    /// <param name=\"parent\">The parent Azure Cosmos DB database resource.</param>\n    public AzureCosmosDBContainerResource(string name, string containerName, string partitionKeyPath, AzureCosmosDBDatabaseResource parent) : base(name)\n    {\n        ArgumentException.ThrowIfNullOrEmpty(containerName);\n        ArgumentException.ThrowIfNullOrEmpty(partitionKeyPath);\n        ContainerProperties = new ContainerProperties(containerName, partitionKeyPath);\n        Parent = parent ?? throw new ArgumentNullException(nameof(parent));\n    }\n\n    /// <summary>","sourceCodeStart":25,"sourceCodeEnd":61,"githubUrl":"https://github.com/microsoft/aspire/blob/25830f84bd145686607ad00c057b3f84e2e51d43/src/Aspire.Hosting.Azure.CosmosDB/AzureCosmosDBContainerResource.cs#L25-L61","documentation":"A container resource must belong to an AzureCosmosDBDatabaseResource; the multi-path constructor checks parent for null and throws ArgumentNullException with parameter name 'parent'. The Parent property drives model navigation, so a null parent is invalid by construction.","triggerScenarios":"Directly invoking new AzureCosmosDBContainerResource(name, containerName, paths, null!) or passing a database variable that is null because an earlier lookup (e.g. builder.Resources.OfType<...>().FirstOrDefault()) found nothing.","commonSituations":"Using the null-forgiving operator on a nullable database reference; calling AddContainer with a database returned from a failed lookup; refactoring that removed database creation but left container creation.","solutions":["Pass a real AzureCosmosDBDatabaseResource, e.g. var db = cosmos.AddDatabase(\"mydb\"); then create the container against db.","If the database may be absent, check it for null before constructing the container and surface your own error.","Prefer the fluent builder API (cosmos.AddDatabase(...).AddContainer(...)) which guarantees a valid parent."],"exampleFix":"// before\nAddContainer(db /* null */, \"orders\", paths);\n// after\nvar db = cosmos.AddDatabase(\"mydb\");\nAddContainer(db, \"orders\", paths);","handlingStrategy":"type-guard","validationCode":"if (db is null)\n{\n    throw new InvalidOperationException(\"Container requires a non-null parent AzureCosmosDBDatabaseResource.\");\n}","typeGuard":"bool hasParent = db is AzureCosmosDBDatabaseResource;","tryCatchPattern":"try\n{\n    var container = new AzureCosmosDBContainerResource(name, containerName, paths, db);\n}\ncatch (ArgumentNullException ex) when (ex.ParamName == \"parent\")\n{\n    logger.LogError(ex, \"Parent database resource was null when creating container {Container}.\", containerName);\n    throw;\n}","preventionTips":["Null-check database lookups (FirstOrDefault) before using the result.","Avoid null! casts on database references.","Use cosmos.AddDatabase(...).AddContainer(...) fluent chains to guarantee a valid parent."],"tags":["null-argument","cosmosdb","azure","constructor"],"backgroundTag":"null-argument","analyzedSha":"25830f84bd145686607ad00c057b3f84e2e51d43","analyzedAt":"2026-09-16T11:10:06.193Z","contentChangedAt":"2026-09-16T11:10:06.193Z","schemaVersion":2},"datasetVersion":"2026-09-21T09:17:21.228Z"}