{"record":{"id":"15da81562bae81bf","repo":"dotnet/efcore","slug":"the-query-uses-notrackingwithidentityresolution","errorCode":null,"errorMessage":"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.","messagePattern":"The query uses 'NoTrackingWithIdentityResolution' and projects owned entities, but the projection does not include the primary key properties '(.+?)' of the root document entity type '(.+?)'\\. Include all primary key properties of the root document entity in the projection\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/EFCore.Cosmos/Query/Internal/CosmosQueryTranslationPostprocessor.cs","lineNumber":97,"sourceCode":"        {\n            return;\n        }\n\n        var visitor = new NoTrackingIdentityResolutionProjectionVisitor(selectExpression, rootEntityType, primaryKeyProperties);\n        visitor.Visit(shaperExpression);\n\n        if (!visitor.ContainsOwnedEntityShaper)\n        {\n            return;\n        }\n\n        var missingKeyProperties = primaryKeyProperties\n            .Where(property => !visitor.ProjectedKeyProperties.Contains(property))\n            .ToArray();\n\n        if (missingKeyProperties.Length > 0)\n        {\n            throw new InvalidOperationException(\n                CosmosStrings.NoTrackingIdentityResolutionOwnedEntityProjectionMissingOwnerKey(\n                    string.Join(\", \", missingKeyProperties.Select(property => property.Name)),\n                    rootEntityType.DisplayName()));\n        }\n    }\n\n    private sealed class NoTrackingIdentityResolutionProjectionVisitor(\n        SelectExpression selectExpression,\n        IEntityType rootEntityType,\n        IReadOnlyList<IProperty> primaryKeyProperties)\n        : ExpressionVisitor\n    {\n        public bool ContainsOwnedEntityShaper { get; private set; }\n\n        public HashSet<IProperty> ProjectedKeyProperties { get; } = [];\n\n        public override Expression? Visit(Expression? node)\n            => ContainsOwnedEntityShaper","sourceCodeStart":79,"sourceCodeEnd":115,"githubUrl":"https://github.com/dotnet/efcore/blob/3a2006ef569de08368d59db5e1468aa8f407e4f8/src/EFCore.Cosmos/Query/Internal/CosmosQueryTranslationPostprocessor.cs#L79-L115","documentation":"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.","triggerScenarios":"A query that uses .AsNoTrackingWithIdentityResolution() and selects only owned navigations or a subset of properties, omitting the owner's PK columns from the projection.","commonSituations":"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.","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."],"exampleFix":"// before\nvar q = db.Orders.AsNoTrackingWithIdentityResolution()\n    .Select(o => o.ShippingAddress);\n// after\nvar q = db.Orders.AsNoTrackingWithIdentityResolution()\n    .Select(o => new { o.Id, o.ShippingAddress });","handlingStrategy":"validation","validationCode":"// prefer AsNoTracking for owned-only projections; assert identity resolution is really needed\nstatic IQueryable<T> OwnedProjection<T>(IQueryable<T> q, bool needIdentityResolution)\n    where T : class\n    => needIdentityResolution ? q.AsNoTrackingWithIdentityResolution() : q.AsNoTracking();","typeGuard":null,"tryCatchPattern":null,"preventionTips":["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."],"tags":["cosmos","owned-entities","no-tracking","identity-resolution","projection"],"backgroundTag":null,"analyzedSha":"3a2006ef569de08368d59db5e1468aa8f407e4f8","analyzedAt":"2026-08-11T23:42:04.146Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}