{"record":{"id":"00db0f69609b8ecb","repo":"dotnet/efcore","slug":"unable-to-generate-a-valid-id-value-to-execute-a","errorCode":null,"errorMessage":"Unable to generate a valid 'id' value to execute a 'ReadItem' query. This usually happens when the value provided for one of the properties is 'null' or an empty string. Provide a value that's not 'null' or an empty string.","messagePattern":"Unable to generate a valid 'id' value to execute a 'ReadItem' query\\. This usually happens when the value provided for one of the properties is 'null' or an empty string\\. Provide a value that's not 'null' or an empty string\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/EFCore.Cosmos/Query/Internal/CosmosShapedQueryCompilingExpressionVisitor.ReadItemQueryingEnumerable.cs","lineNumber":94,"sourceCode":"            Check.DebugAssert(\n                jsonIdDefinition != null,\n                \"Should not be using this enumerable if not using ReadItem, which needs an id definition.\");\n\n            var values = new List<object>(jsonIdDefinition.Properties.Count);\n            foreach (var property in jsonIdDefinition.Properties)\n            {\n                var value = _readItemInfo.PropertyValues[property] switch\n                {\n                    SqlParameterExpression { Name: var parameterName } => _cosmosQueryContext.Parameters[parameterName],\n                    SqlConstantExpression { Value: var constantValue } => constantValue,\n                    _ => throw new UnreachableException()\n                };\n\n                values.Add(value);\n            }\n\n            resourceId = jsonIdDefinition.GenerateIdString(values);\n            return string.IsNullOrEmpty(resourceId) ? throw new InvalidOperationException(CosmosStrings.InvalidResourceId) : true;\n        }\n\n        private sealed class AsyncEnumerator : IAsyncEnumerator<T>\n        {\n            private readonly CosmosQueryContext _cosmosQueryContext;\n            private readonly string _cosmosContainer;\n            private readonly PartitionKey _cosmosPartitionKey;\n            private readonly Shaper<T> _shaper;\n            private readonly Type _contextType;\n            private readonly IDiagnosticsLogger<DbLoggerCategory.Query> _queryLogger;\n            private readonly bool _standAloneStateManager;\n            private readonly IConcurrencyDetector _concurrencyDetector;\n            private readonly IExceptionDetector _exceptionDetector;\n            private readonly ReadItemQueryingEnumerable<T> _readItemEnumerable;\n            private readonly CancellationToken _cancellationToken;\n\n            private ReadOnlyMemory<byte>? _response;\n            private bool _hasExecuted;","sourceCodeStart":76,"sourceCodeEnd":112,"githubUrl":"https://github.com/dotnet/efcore/blob/dbf9771522148d61a2467854921bd5dc6f6e6916/src/EFCore.Cosmos/Query/Internal/CosmosShapedQueryCompilingExpressionVisitor.ReadItemQueryingEnumerable.cs#L76-L112","documentation":"Thrown by TryGetResourceId when the generated Cosmos resource 'id' string is null or empty. The id is composed from the entity's jsonIdDefinition properties; if one of those property values is null or an empty string, the composed id is invalid and the ReadItem query cannot be executed.","triggerScenarios":"A ReadItem-optimized query where one of the properties contributing to the document id is null or string.Empty at execution time. This happens when the id is composed of multiple PK parts and one part is missing.","commonSituations":"Entities whose id is built from a composite of properties where a non-Id segment (e.g. a discriminator or region prefix) is null. Data where a key property was not set before querying. Configuring a JsonIdDefinition that includes a nullable property.","solutions":["Ensure all properties contributing to the id definition are non-null and non-empty before executing the query.","Validate key values at the application boundary and reject null/empty early.","Review the entity's id composition (HasId/partition key mapping) to ensure only required, always-set properties are included.","Provide explicit parameter values rather than relying on defaults that may be null."],"exampleFix":"// before\nvar id = Guid.NewGuid().ToString();\nvar region = string.Empty;\nvar blog = await context.Blogs.WithPartitionKey(region).Where(b => b.Id == id).FirstAsync();\n// after\nvar id = Guid.NewGuid().ToString();\nvar region = \"us-east\"; // ensure non-empty\nvar blog = await context.Blogs.WithPartitionKey(region).Where(b => b.Id == id).FirstAsync();","handlingStrategy":"validation","validationCode":"// Validate all id-defining property values before querying\nforeach (var idProp in entity.GetJsonIdDefinition().Properties)\n{\n    var v = GetValue(idProp);\n    if (v is null or \"\") throw new ArgumentException($\"{idProp} must be non-null/non-empty for a ReadItem query\");\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Validate key/id properties at the application boundary before querying.","Ensure id-defining properties are non-nullable and always set.","Add a guard in repository code that constructs ReadItem-style queries."],"tags":["cosmos","read-item","id","validation","query"],"analyzedSha":"dbf9771522148d61a2467854921bd5dc6f6e6916","analyzedAt":"2026-08-06T20:46:03.226Z","schemaVersion":2},"datasetVersion":"2026-08-07T03:17:09.362Z"}