dotnet/efcore · error · InvalidOperationException

The entity of type '{entityType}' is mapped as a part of the

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 error 135, but thrown when EnableSensitiveDataLogging is NOT enabled (CosmosDatabaseWrapper.cs:570-573). The message omits the key value for data-protection reasons and instead suggests enabling EnableSensitiveDataLogging to see the key values for diagnosis. The root cause is identical: an owned/embedded entity has no tracked principal.

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 dbf9771522)

Solutions

  1. Track the principal entity before saving the owned entity (same as error 135).
  2. Temporarily enable optionsBuilder.EnableSensitiveDataLogging() in development/staging to see which key value is involved, then fix the graph.
  3. Use context.Attach(parent) or load the parent from the context before modifying the owned entity.

Example fix

// before
var addr = JsonConvert.DeserializeObject<Address>(json);
context.Update(addr); // owned entity, parent not tracked
await context.SaveChangesAsync(); // throws (no key value shown)

// after — enable sensitive logging to diagnose, then fix the graph
optionsBuilder.EnableSensitiveDataLogging(); // dev only
// fix: always load and track the parent
var user = await context.Users.FindAsync(userId);
user.HomeAddress = addr;
await context.SaveChangesAsync();
Defensive patterns

Strategy: validation

Validate before calling

// Same as 135: verify owned entities have tracked principals before SaveChanges.
var owned = context.ChangeTracker.Entries()
    .Where(e => !e.EntityType.IsDocumentRoot() && e.State is EntityState.Added or EntityState.Modified or EntityState.Deleted);
foreach (var entry in owned)
{
    var ownership = entry.EntityType.FindOwnership();
    if (ownership is null) continue;
    // verify the principal is tracked
}

Prevention

When it happens

Trigger: Same as error 135: saving an owned entity whose parent is not tracked in the current DbContext. The change tracker cannot locate the root document to serialize the owned entity into.

Common situations: Production environment where sensitive data logging is disabled, making the error harder to diagnose. Owned entities received from API payloads without their parent. Test or batch code that processes owned entities in isolation.

Related errors


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