{"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/dbf9771522148d61a2467854921bd5dc6f6e6916/src/EFCore.Cosmos/Query/Internal/CosmosQueryTranslationPostprocessor.cs#L79-L115","documentation":"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.","triggerScenarios":"Calling .AsNoTrackingWithIdentityResolution() and then .Select() projecting only owned/nested entities or a subset of properties that excludes the root entity's primary key columns.","commonSituations":"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.","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."],"exampleFix":"// before\nvar result = await context.Blogs\n    .AsNoTrackingWithIdentityResolution()\n    .Select(b => new { b.OwnedDetails })\n    .ToListAsync();\n// after\nvar result = await context.Blogs\n    .AsNoTrackingWithIdentityResolution()\n    .Select(b => new { b.Id, b.OwnedDetails })\n    .ToListAsync();","handlingStrategy":"validation","validationCode":"// Before executing, ensure the root PK is in the projection\nvar pkProps = context.Model.FindEntityType(typeof(Blog))!.FindPrimaryKey()!.Properties;\n// include all pkProps names in your Select projection","typeGuard":null,"tryCatchPattern":null,"preventionTips":["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."],"tags":["cosmos","no-tracking","identity-resolution","owned-entity","projection","query"],"analyzedSha":"dbf9771522148d61a2467854921bd5dc6f6e6916","analyzedAt":"2026-08-06T20:46:03.226Z","schemaVersion":2},"datasetVersion":"2026-08-07T03:17:09.362Z"}