{"record":{"id":"da153dacd2c50c71","repo":"dotnet/efcore","slug":"the-entity-of-type-entitytype-is-mapped-as-a-p","errorCode":null,"errorMessage":"The entity of type '{entityType}' is mapped as a part of the document mapped to '{missingEntityType}', but there is no tracked entity of this type with the corresponding key value. Consider using 'DbContextOptionsBuilder.EnableSensitiveDataLogging' to see the key values.","messagePattern":"The entity of type '(.+?)' is mapped as a part of the document mapped to '(.+?)', but there is no tracked entity of this type with the corresponding key value\\. Consider using 'DbContextOptionsBuilder\\.EnableSensitiveDataLogging' to see the key values\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/EFCore.Cosmos/Storage/Internal/CosmosDatabaseWrapper.cs","lineNumber":570,"sourceCode":"#pragma warning disable EF1001 // Internal EF Core API usage.\n    // Issue #16707\n    private IUpdateEntry GetRootDocument(InternalEntityEntry entry)\n    {\n        var stateManager = entry.StateManager;\n        var ownership = entry.EntityType.FindOwnership()!;\n        var principal = stateManager.FindPrincipal(entry, ownership);\n        if (principal == null)\n        {\n            if (_sensitiveLoggingEnabled)\n            {\n                throw new InvalidOperationException(\n                    CosmosStrings.OrphanedNestedDocumentSensitive(\n                        entry.EntityType.DisplayName(),\n                        ownership.PrincipalEntityType.DisplayName(),\n                        entry.BuildCurrentValuesString(entry.EntityType.FindPrimaryKey()!.Properties)));\n            }\n\n            throw new InvalidOperationException(\n                CosmosStrings.OrphanedNestedDocument(\n                    entry.EntityType.DisplayName(),\n                    ownership.PrincipalEntityType.DisplayName()));\n        }\n\n        return principal.EntityType.IsDocumentRoot() ? principal : GetRootDocument(principal);\n    }\n#pragma warning restore EF1001 // Internal EF Core API usage.\n\n    private DbUpdateException WrapUpdateException(Exception exception, IReadOnlyList<IUpdateEntry> entries)\n    {\n        var entry = entries[0];\n        var serializer = _structuralTypeSerializerProvider.Get((entry.SharedIdentityEntry ?? entry).EntityType);\n        var id = serializer.GetJsonId(entry.SharedIdentityEntry ?? entry);\n\n        return CosmosClientWrapper.WrapUpdateException(exception, id, entries);\n    }\n","sourceCodeStart":552,"sourceCodeEnd":588,"githubUrl":"https://github.com/dotnet/efcore/blob/3a2006ef569de08368d59db5e1468aa8f407e4f8/src/EFCore.Cosmos/Storage/Internal/CosmosDatabaseWrapper.cs#L552-L588","documentation":"Same orphaned-nested-document condition as the sensitive variant, but thrown when sensitive data logging is disabled. Without the key value, the message tells you to enable EnableSensitiveDataLogging to see the offending key. FindPrincipal returned null for a nested owned entity.","triggerScenarios":"An owned dependent entity is tracked without its principal being tracked, and dbContextOptionsBuilder.EnableSensitiveDataLogging() has not been called. See CosmosDatabaseWrapper.cs:570 (the non-sensitive throw).","commonSituations":"Production builds (where sensitive logging is off) hitting the orphaned-dependent condition described above. First-time diagnosis of a broken ownership graph.","solutions":["Temporarily enable EnableSensitiveDataLogging() in a dev/test environment to capture the key value of the orphaned entity.","Ensure the principal entity is tracked before SaveChanges (Attach/Add the parent).","Audit the code that constructs or detaches the ownership graph to find where the parent link is lost."],"exampleFix":"// before (diagnosis)\noptionsBuilder.UseCosmos(...);\n// throw shows no key value\n\n// after (dev only - to capture the key)\noptionsBuilder.EnableSensitiveDataLogging().EnableSensitiveDataLogging();\n// then fix the root cause: track the principal before saving","handlingStrategy":"validation","validationCode":"// Run this same principal-tracking check (see 129) in a dev build with\n// EnableSensitiveDataLogging enabled so you can see the key value.\noptionsBuilder.EnableSensitiveDataLogging();","typeGuard":null,"tryCatchPattern":"try { await dbContext.SaveChangesAsync(ct); }\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"no tracked entity of this type\"))\n{\n    // enable sensitive logging, reproduce, then fix the missing principal link.\n}","preventionTips":["Enable sensitive data logging in development to get the key value immediately.","Track principals before dependents.","Add an integration test that exercises the full ownership graph."],"tags":["cosmos","owned-entity","change-tracking","diagnostics","savechanges"],"backgroundTag":null,"analyzedSha":"3a2006ef569de08368d59db5e1468aa8f407e4f8","analyzedAt":"2026-08-11T23:42:04.146Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}