dotnet/efcore · error · InvalidOperationException
Unable to execute a 'ReadItem' query since the 'id' value…
Error message
Unable to execute a 'ReadItem' query since the 'id' value is missing and cannot be generated.
What it means
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.
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.
Example fix
// before var u = await db.Users.FindAsync(partitionKey, id); // id sometimes null // after if (id is null) return null; var u = await db.Users.FindAsync(partitionKey, id);
Defensive patterns
Strategy: validation
Validate before calling
static async Task<T?> FindSafeAsync<T>(DbSet<T> set, object[] keys, CancellationToken ct)
where T : class
{
if (keys.Any(k => k is null || (k is string s && s.Length == 0)))
throw new ArgumentException("all key parts must be non-null/non-empty", nameof(keys));
return await set.FindAsync(keys, ct).AsTask().ConfigureAwait(false);
} Type guard
static bool AllKeyPartsPresent(object[] keys)
=> keys.Length > 0 && keys.All(k => k is not null && (k is not string s || s.Length > 0)); Prevention
- 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.
When it happens
Trigger: 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.).
Common situations: Changing the entity key configuration after deployment, projecting only some key properties, or passing a key value tuple where one element is missing.
Related errors
- Unable to generate a valid 'id' value to execute a…
- Azure Cosmos DB does not support synchronous I/O. Make sure…
- A call was made to ' ' that changed an option that must be…
- A FromSqlExpression has an invalid arguments expression type
- A full-text index is defined for
AI-assisted analysis of dotnet/efcore@3a2006ef56 (2026-08-11).
Data as JSON: /api/errors/53deefeff1dbcdf6.
Report an issue: GitHub.
Appendix: source
Thrown at src/EFCore.Cosmos/Query/Internal/CosmosShapedQueryCompilingExpressionVisitor.ReadItemQueryingEnumerable.cs:147
: null;
}
public T Current { get; private set; }
public async ValueTask<bool> MoveNextAsync()
{
try
{
using var _ = _concurrencyDetector?.EnterCriticalSection();
if (_hasExecuted)
{
return false;
}
if (!_readItemEnumerable.TryGetResourceId(out var resourceId))
{
throw new InvalidOperationException(CosmosStrings.ResourceIdMissing);
}
EntityFrameworkMetricsData.ReportQueryExecuting();
_response = await _cosmosQueryContext.CosmosClient.ExecuteReadItemAsync(
_cosmosContainer,
_cosmosPartitionKey,
resourceId,
_cosmosQueryContext.SessionTokenStorage,
_cancellationToken)
.ConfigureAwait(false);
return ShapeResult();
}
catch (Exception exception)
{
if (_exceptionDetector.IsCancellation(exception, _cancellationToken))
{View on GitHub (pinned to 3a2006ef56)