Repository: dotnet/efcore
File: src/EFCore.Relational/Update/Internal/SharedTableEntryMap.cs (C#)

Code before:
    }

    /// <summary>
    ///     This is an internal API that supports the Entity Framework Core infrastructure and not subject to
    ///     the same compatibility standards as public APIs. It may be changed or removed without notice in
    ///     any release. You should only use it directly in your code with extreme caution and knowing that
    ///     doing so can result in application failures when updating to a new Entity Framework Core release.
    /// </summary>
    public virtual bool IsMainEntry(IUpdateEntry entry)
        => !_table.GetRowInternalForeignKeys(entry.EntityType).Any();

    private IUpdateEntry GetMainEntry(IUpdateEntry entry)
    {
        var entityType = entry.EntityType;
        var foreignKeys = _table.GetRowInternalForeignKeys(entityType);
        foreach (var foreignKey in foreignKeys)
        {
            var principalEntry = _updateAdapter.FindPrincipal(entry, foreignKey);
            if (principalEntry != null)
            {
                return GetMainEntry(principalEntry);
            }

            // When TPH entity replacement causes FindPrincipal to return null (the replacing entry
            // is an incompatible sibling type), check if the replacing entry's SharedIdentityEntry
            // is a compatible principal, and use the replacing entry as the main entry.
            if (principalEntry == null)
            {
                var keyValues = foreignKey.Properties.Select(p => entry.GetCurrentValue(p)).ToArray();

                var identityEntry = _updateAdapter.TryGetEntry(foreignKey.PrincipalKey, keyValues);
                if (identityEntry?.SharedIdentityEntry != null
                    && foreignKey.PrincipalEntityType.IsAssignableFrom(identityEntry.SharedIdentityEntry.EntityType))
                {
                    return GetMainEntry(identityEntry);
                }
            }
        }

        return entry;
    }

    /// <summary>
    ///     This is an internal API that supports the Entity Framework Core infrastructure and not subject to
    ///     the same compatibility standards as public APIs. It may be changed or removed without notice in
    ///     any release. You should only use it directly in your code with extreme caution and knowing that
    ///     doing so can result in application failures when updating to a new Entity Framework Core release.
    /// </summary>
    public virtual IReadOnlyList<IUpdateEntry> GetAllEntries(IUpdateEntry entry)
    {
        var entries = new List<IUpdateEntry>();

Review comment (suggestion):
@copilot 
```suggestion
            // When TPH entity replacement causes FindPrincipal to return null, check if the replacing
            // entry's SharedIdentityEntry has a compatible principal and use it as the main entry.
```

Code after:
    }

    /// <summary>
    ///     This is an internal API that supports the Entity Framework Core infrastructure and not subject to
    ///     the same compatibility standards as public APIs. It may be changed or removed without notice in
    ///     any release. You should only use it directly in your code with extreme caution and knowing that
    ///     doing so can result in application failures when updating to a new Entity Framework Core release.
    /// </summary>
    public virtual bool IsMainEntry(IUpdateEntry entry)
        => !_table.GetRowInternalForeignKeys(entry.EntityType).Any();

    private IUpdateEntry GetMainEntry(IUpdateEntry entry)
    {
        var entityType = entry.EntityType;
        var foreignKeys = _table.GetRowInternalForeignKeys(entityType);
        foreach (var foreignKey in foreignKeys)
        {
            var principalEntry = _updateAdapter.FindPrincipal(entry, foreignKey);
            if (principalEntry != null)
            {
                return GetMainEntry(principalEntry);
            }

            // When TPH entity replacement causes FindPrincipal to return null, check if the replacing
            // entry's SharedIdentityEntry has a compatible principal and use it as the main entry.
            var keyValues = foreignKey.Properties.Select(p => entry.GetCurrentValue(p)).ToArray();
            var replacingPrincipal = _updateAdapter.TryGetEntry(foreignKey.PrincipalKey, keyValues);
            if (replacingPrincipal?.SharedIdentityEntry != null
                && foreignKey.PrincipalEntityType.IsAssignableFrom(replacingPrincipal.SharedIdentityEntry.EntityType))
            {
                return GetMainEntry(replacingPrincipal);
            }
        }

        return entry;
    }

    /// <summary>
    ///     This is an internal API that supports the Entity Framework Core infrastructure and not subject to
    ///     the same compatibility standards as public APIs. It may be changed or removed without notice in
    ///     any release. You should only use it directly in your code with extreme caution and knowing that
    ///     doing so can result in application failures when updating to a new Entity Framework Core release.
    /// </summary>
    public virtual IReadOnlyList<IUpdateEntry> GetAllEntries(IUpdateEntry entry)
    {
        var entries = new List<IUpdateEntry>();
        AddAllDependentsInclusive(GetMainEntry(entry), entries);

        return entries;
    }