{"record":{"id":"53deefeff1dbcdf6","repo":"dotnet/efcore","slug":"unable-to-execute-a-readitem-query-since-the-id","errorCode":null,"errorMessage":"Unable to execute a 'ReadItem' query since the 'id' value is missing and cannot be generated.","messagePattern":"Unable to execute a 'ReadItem' query since the 'id' value is missing and cannot be generated\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/EFCore.Cosmos/Query/Internal/CosmosShapedQueryCompilingExpressionVisitor.ReadItemQueryingEnumerable.cs","lineNumber":147,"sourceCode":"                    : null;\n            }\n\n            public T Current { get; private set; }\n\n            public async ValueTask<bool> MoveNextAsync()\n            {\n                try\n                {\n                    using var _ = _concurrencyDetector?.EnterCriticalSection();\n\n                    if (_hasExecuted)\n                    {\n                        return false;\n                    }\n\n                    if (!_readItemEnumerable.TryGetResourceId(out var resourceId))\n                    {\n                        throw new InvalidOperationException(CosmosStrings.ResourceIdMissing);\n                    }\n\n                    EntityFrameworkMetricsData.ReportQueryExecuting();\n\n                    _response = await _cosmosQueryContext.CosmosClient.ExecuteReadItemAsync(\n                            _cosmosContainer,\n                            _cosmosPartitionKey,\n                            resourceId,\n                            _cosmosQueryContext.SessionTokenStorage,\n                            _cancellationToken)\n                        .ConfigureAwait(false);\n\n                    return ShapeResult();\n                }\n                catch (Exception exception)\n                {\n                    if (_exceptionDetector.IsCancellation(exception, _cancellationToken))\n                    {","sourceCodeStart":129,"sourceCodeEnd":165,"githubUrl":"https://github.com/dotnet/efcore/blob/3a2006ef569de08368d59db5e1468aa8f407e4f8/src/EFCore.Cosmos/Query/Internal/CosmosShapedQueryCompilingExpressionVisitor.ReadItemQueryingEnumerable.cs#L129-L165","documentation":"During MoveNextAsync of a ReadItem enumeration, the enumerator calls TryGetResourceId; if id generation produces no usable resource id it throws InvalidOperationException ResourceIdMissing. Distinct from InvalidResourceId: this covers the path where the id cannot even be assembled for the request.","triggerScenarios":"A ReadItem-shaped query executes against an entity whose key configuration or property values prevent generating an id (missing key property in projection, null constituent that short-circuits id generation, etc.).","commonSituations":"Changing the entity key configuration after deployment, projecting only some key properties, or passing a key value tuple where one element is missing.","solutions":["Verify the entity's primary key is fully configured and all key properties are populated before querying.","Ensure the key properties used for ReadItem optimization are included in the query predicate.","If a value can be missing at runtime, fall back to a SQL query (Where clause) instead of relying on ReadItem."],"exampleFix":"// before\nvar u = await db.Users.FindAsync(partitionKey, id);  // id sometimes null\n// after\nif (id is null) return null;\nvar u = await db.Users.FindAsync(partitionKey, id);","handlingStrategy":"validation","validationCode":"static async Task<T?> FindSafeAsync<T>(DbSet<T> set, object[] keys, CancellationToken ct)\n    where T : class\n{\n    if (keys.Any(k => k is null || (k is string s && s.Length == 0)))\n        throw new ArgumentException(\"all key parts must be non-null/non-empty\", nameof(keys));\n    return await set.FindAsync(keys, ct).AsTask().ConfigureAwait(false);\n}","typeGuard":"static bool AllKeyPartsPresent(object[] keys)\n    => keys.Length > 0 && keys.All(k => k is not null && (k is not string s || s.Length > 0));","tryCatchPattern":null,"preventionTips":["Keep key property configuration stable; avoid runtime key changes.","Pass fully-populated key tuples to Find.","Log the entity type and key values when a lookup fails to generate an id."],"tags":["cosmos","readitem","keys","query-execution"],"backgroundTag":null,"analyzedSha":"3a2006ef569de08368d59db5e1468aa8f407e4f8","analyzedAt":"2026-08-11T23:42:04.146Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}