dotnet/efcore · error · InvalidOperationException

The entity of type ' ' is mapped as a part of the document…

Error message

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.

What it means

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.

Solutions

  1. Temporarily enable EnableSensitiveDataLogging() in a dev/test environment to capture the key value of the orphaned entity.
  2. Ensure the principal entity is tracked before SaveChanges (Attach/Add the parent).
  3. Audit the code that constructs or detaches the ownership graph to find where the parent link is lost.

Example fix

// before (diagnosis)
optionsBuilder.UseCosmos(...);
// throw shows no key value

// after (dev only - to capture the key)
optionsBuilder.EnableSensitiveDataLogging().EnableSensitiveDataLogging();
// then fix the root cause: track the principal before saving
Defensive patterns

Strategy: validation

Validate before calling

// Run this same principal-tracking check (see 129) in a dev build with
// EnableSensitiveDataLogging enabled so you can see the key value.
optionsBuilder.EnableSensitiveDataLogging();

Try / catch

try { await dbContext.SaveChangesAsync(ct); }
catch (InvalidOperationException ex) when (ex.Message.Contains("no tracked entity of this type"))
{
    // enable sensitive logging, reproduce, then fix the missing principal link.
}

Prevention

When it happens

Trigger: 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).

Common situations: Production builds (where sensitive logging is off) hitting the orphaned-dependent condition described above. First-time diagnosis of a broken ownership graph.

Related errors


AI-assisted analysis of dotnet/efcore@3a2006ef56 (2026-08-11). Data as JSON: /api/errors/da153dacd2c50c71. Report an issue: GitHub.

Appendix: source

Thrown at src/EFCore.Cosmos/Storage/Internal/CosmosDatabaseWrapper.cs:570

#pragma warning disable EF1001 // Internal EF Core API usage.
    // Issue #16707
    private IUpdateEntry GetRootDocument(InternalEntityEntry entry)
    {
        var stateManager = entry.StateManager;
        var ownership = entry.EntityType.FindOwnership()!;
        var principal = stateManager.FindPrincipal(entry, ownership);
        if (principal == null)
        {
            if (_sensitiveLoggingEnabled)
            {
                throw new InvalidOperationException(
                    CosmosStrings.OrphanedNestedDocumentSensitive(
                        entry.EntityType.DisplayName(),
                        ownership.PrincipalEntityType.DisplayName(),
                        entry.BuildCurrentValuesString(entry.EntityType.FindPrimaryKey()!.Properties)));
            }

            throw new InvalidOperationException(
                CosmosStrings.OrphanedNestedDocument(
                    entry.EntityType.DisplayName(),
                    ownership.PrincipalEntityType.DisplayName()));
        }

        return principal.EntityType.IsDocumentRoot() ? principal : GetRootDocument(principal);
    }
#pragma warning restore EF1001 // Internal EF Core API usage.

    private DbUpdateException WrapUpdateException(Exception exception, IReadOnlyList<IUpdateEntry> entries)
    {
        var entry = entries[0];
        var serializer = _structuralTypeSerializerProvider.Get((entry.SharedIdentityEntry ?? entry).EntityType);
        var id = serializer.GetJsonId(entry.SharedIdentityEntry ?? entry);

        return CosmosClientWrapper.WrapUpdateException(exception, id, entries);
    }

View on GitHub (pinned to 3a2006ef56)