dotnet/efcore · error · InvalidOperationException
The query uses 'NoTrackingWithIdentityResolution' and…
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
Under NoTrackingWithIdentityResolution EF must keep owner identity across projected owned entities, so it needs the root entity's full primary key in the projection. The postprocessor walks the projection, collects ProjectedKeyProperties, and if any primary key property of the root document entity is missing it throws InvalidOperationException.
Solutions
- Include the root entity primary key property(s) in the projection (select the owner or its Id explicitly).
- If identity resolution is not required, use .AsNoTracking() instead of AsNoTrackingWithIdentityResolution.
- If you only need owned data, project the root entity type and then map to a DTO client-side.
Example fix
// before
var q = db.Orders.AsNoTrackingWithIdentityResolution()
.Select(o => o.ShippingAddress);
// after
var q = db.Orders.AsNoTrackingWithIdentityResolution()
.Select(o => new { o.Id, o.ShippingAddress }); Defensive patterns
Strategy: validation
Validate before calling
// prefer AsNoTracking for owned-only projections; assert identity resolution is really needed
static IQueryable<T> OwnedProjection<T>(IQueryable<T> q, bool needIdentityResolution)
where T : class
=> needIdentityResolution ? q.AsNoTrackingWithIdentityResolution() : q.AsNoTracking(); Prevention
- Always include the owner's PK when projecting owned entities under identity resolution.
- Default to AsNoTracking; promote to WithIdentityResolution only when needed.
- Add a test asserting the projected property set covers FindPrimaryKey().Properties.
When it happens
Trigger: A query that uses .AsNoTrackingWithIdentityResolution() and selects only owned navigations or a subset of properties, omitting the owner's PK columns from the projection.
Common situations: Projection DTOs that pull owned sub-objects but not the parent id, switching from AsNoTracking to AsNoTrackingWithIdentityResolution on an existing projection query, or partial entity selects.
Related errors
- The LINQ expression ' ' could not be translated. Either…
- Complex projections in subqueries are currently unsupported.
- Invalid token type: ' '.
- The LINQ expression ' ' could not be translated. Either…
- The specified entity type
AI-assisted analysis of dotnet/efcore@3a2006ef56 (2026-08-11).
Data as JSON: /api/errors/15da81562bae81bf.
Report an issue: GitHub.
Appendix: 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 3a2006ef56)