dotnet/efcore · error · InvalidOperationException
The query uses 'NoTrackingWithIdentityResolution' and projec
Error message
The query uses 'NoTrackingWithIdentityResolution' and projects owned entities, but the projection does not include the primary key properties '{properties}' of the root document entity type '{entityType}'. Include all primary key properties of the root document entity in the projection. What it means
Thrown during query postprocessing when the query uses NoTrackingWithIdentityResolution and the shaper contains owned entity types, but the projection does not include all primary key properties of the root document entity. Identity resolution for owned entities requires the owner's PK to be present in the projection so EF can correctly associate owned instances with their owner; without it EF cannot guarantee correct identity-resolution semantics.
Source
Thrown at src/EFCore.Cosmos/Query/Internal/CosmosQueryTranslationPostprocessor.cs:97
{
return;
}
var visitor = new NoTrackingIdentityResolutionProjectionVisitor(selectExpression, rootEntityType, primaryKeyProperties);
visitor.Visit(shaperExpression);
if (!visitor.ContainsOwnedEntityShaper)
{
return;
}
var missingKeyProperties = primaryKeyProperties
.Where(property => !visitor.ProjectedKeyProperties.Contains(property))
.ToArray();
if (missingKeyProperties.Length > 0)
{
throw new InvalidOperationException(
CosmosStrings.NoTrackingIdentityResolutionOwnedEntityProjectionMissingOwnerKey(
string.Join(", ", missingKeyProperties.Select(property => property.Name)),
rootEntityType.DisplayName()));
}
}
private sealed class NoTrackingIdentityResolutionProjectionVisitor(
SelectExpression selectExpression,
IEntityType rootEntityType,
IReadOnlyList<IProperty> primaryKeyProperties)
: ExpressionVisitor
{
public bool ContainsOwnedEntityShaper { get; private set; }
public HashSet<IProperty> ProjectedKeyProperties { get; } = [];
public override Expression? Visit(Expression? node)
=> ContainsOwnedEntityShaperView on GitHub (pinned to dbf9771522)
Solutions
- Include the root entity's primary key property(ies) in the Select projection, e.g. Select(b => new { b.Id, b.Owned }).
- If you do not need identity resolution, use AsNoTracking() instead of AsNoTrackingWithIdentityResolution().
- Project the entire root entity rather than only owned sub-parts.
Example fix
// before
var result = await context.Blogs
.AsNoTrackingWithIdentityResolution()
.Select(b => new { b.OwnedDetails })
.ToListAsync();
// after
var result = await context.Blogs
.AsNoTrackingWithIdentityResolution()
.Select(b => new { b.Id, b.OwnedDetails })
.ToListAsync(); Defensive patterns
Strategy: validation
Validate before calling
// Before executing, ensure the root PK is in the projection var pkProps = context.Model.FindEntityType(typeof(Blog))!.FindPrimaryKey()!.Properties; // include all pkProps names in your Select projection
Prevention
- When using AsNoTrackingWithIdentityResolution with owned entities, always include the owner's PK in Select.
- Prefer AsNoTracking() when you do not specifically need identity resolution across owned entities.
- Add an integration test that exercises the projection to catch this at build time.
When it happens
Trigger: Calling .AsNoTrackingWithIdentityResolution() and then .Select() projecting only owned/nested entities or a subset of properties that excludes the root entity's primary key columns.
Common situations: DTO projections that select only an owned collection or specific owned properties while requesting NoTrackingWithIdentityResolution. Switching from AsNoTracking to AsNoTrackingWithIdentityResolution on an existing projection query.
Related errors
- The LINQ expression '{expression}' could not be translated.
- The LINQ expression '{expression}' could not be translated.
- The LINQ expression '{expression}' could not be translated.
- Including navigation '{navigation}' is not supported as the
- Unhandled expression '{expression}' of type '{expressionType
AI-assisted analysis of dotnet/efcore@dbf9771522 (2026-08-06).
Data as JSON: /api/errors/15da81562bae81bf.
Report an issue: GitHub.