{"record":{"id":"6dad1acbee44a855","repo":"dotnet/efcore","slug":"the-entity-of-type-entitytype-is-mapped-as-par","errorCode":null,"errorMessage":"The entity of type '{entityType}' is mapped as part of the document mapped to '{missingEntityType}', but there is no tracked entity of this type with the key value '{keyValue}'.","messagePattern":"The entity of type '(.+?)' is mapped as part of the document mapped to '(.+?)', but there is no tracked entity of this type with the key value '(.+?)'\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/EFCore.Cosmos/Storage/Internal/CosmosDatabaseWrapper.cs","lineNumber":563,"sourceCode":"                        updateEntry.Entry.Context, errorEntries, (DbUpdateConcurrencyException)exception, null, cancellationToken)\n                    .ConfigureAwait(false)).IsSuppressed\n                    ? throw exception\n                    : false;\n        }\n    }\n\n#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    {","sourceCodeStart":545,"sourceCodeEnd":581,"githubUrl":"https://github.com/dotnet/efcore/blob/3a2006ef569de08368d59db5e1468aa8f407e4f8/src/EFCore.Cosmos/Storage/Internal/CosmosDatabaseWrapper.cs#L545-L581","documentation":"While preparing a SaveChanges operation, the provider walks up the ownership chain to find the root document for a nested owned entity. FindPrincipal returned null, meaning the parent/principal entity that owns this nested document is not tracked. With sensitive-data logging enabled, the key value of the orphaned entity is included to help diagnose it.","triggerScenarios":"An owned/dependent entity is tracked in the Added/Modified state but its principal entity (the document root or intermediate owner) is not in the ChangeTracker. GetRootDocument at CosmosDatabaseWrapper.cs:563 calls stateManager.FindPrincipal which returns null, and _sensitiveLoggingEnabled is true, so the sensitive overload is thrown.","commonSituations":"Attaching an owned entity without its parent. Deleting the principal while dependents remain tracked. Bugs in change tracking where a navigation was set to null before SaveChanges. Asymmetric deserialization that loses the parent reference.","solutions":["Ensure the principal/parent entity is tracked (Attach/Add it) before calling SaveChanges.","Fix the code that nulls out or detaches the parent navigation before saving.","If the principal was deleted, also remove or detach the dependent entities to keep the graph consistent."],"exampleFix":"// before\nvar order = new Order { Id = 1 };\nvar detail = new OrderDetail { OrderId = 1, ... };\ndbContext.Add(detail); // principal 'order' not tracked\nawait dbContext.SaveChangesAsync();\n\n// after\ndbContext.Add(order);\norder.Details.Add(detail);\nawait dbContext.SaveChangesAsync();","handlingStrategy":"validation","validationCode":"// Before SaveChangesAsync, ensure every owned dependent has a tracked principal\nforeach (var entry in dbContext.ChangeTracker.Entries())\n{\n    var ownership = entry.Metadata.FindOwnership();\n    if (ownership is null) continue;\n    var principal = dbContext.ChangeTracker.Entries()\n        .FirstOrDefault(e => e.Metadata == ownership.PrincipalEntityType\n            && entry.References(ownership.PrincipalToDependent!));\n    if (principal is null)\n        throw new InvalidOperationException($\"{entry.Metadata.DisplayName()} has no tracked principal.\");\n}","typeGuard":null,"tryCatchPattern":"try { await dbContext.SaveChangesAsync(ct); }\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"no tracked entity of this type\"))\n{\n    // Attach/Add the principal, then retry the save.\n}","preventionTips":["Always add or attach the principal before its owned dependents.","Avoid nulling parent navigations before SaveChanges.","Enable sensitive data logging in dev to catch the failing key early."],"tags":["cosmos","owned-entity","change-tracking","sensitive-data-logging","savechanges"],"backgroundTag":null,"analyzedSha":"3a2006ef569de08368d59db5e1468aa8f407e4f8","analyzedAt":"2026-08-11T23:42:04.146Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}