Repository: dotnet/efcore
File: src/EFCore/ChangeTracking/Internal/StateManager.cs (C#)

Code before:
        var principalIsDetached = entry.EntityState == EntityState.Detached;

        var detectChangesEnabled = Context.ChangeTracker.AutoDetectChangesEnabled
            && !((IRuntimeModel)Model).SkipDetectChanges;

        foreignKeys ??= entry.EntityType.GetReferencingForeignKeys();
        foreach (var fk in foreignKeys)
        {
            if (fk.DeleteBehavior == DeleteBehavior.ClientNoAction)
            {
                continue;
            }

            foreach (InternalEntityEntry dependent in (GetDependentsFromNavigation(entry, fk)
                         ?? GetDependents(entry, fk)).ToList())
            {
                if (dependent.SharedIdentityEntry == entry)
                {
                    continue;
                }

                // When an owned entity is replaced (e.g., via record 'with' expression), the old entry is
                // marked Deleted and a new entry with the same key is added via SharedIdentityEntry. If a nested
                // dependent was re-used by the new entity graph, FindPrincipal returns the replacement principal
                // from the identity map rather than the deleted entry. In that case, skip the cascade to avoid
                // incorrectly re-deleting the dependent that now belongs to the replacement principal.
                var currentPrincipal = FindPrincipal(dependent, fk);
                if (currentPrincipal != null && currentPrincipal != entry)
                {
                    continue;
                }

                if (detectChangesEnabled)
                {
                    ChangeDetector.DetectChanges(dependent);
                }

                if (dependent.EntityState != EntityState.Deleted
                    && dependent.EntityState != EntityState.Detached
                    && (dependent.EntityState == EntityState.Added
                        || KeysEqual(entry, fk, dependent)))
                {
                    if (fk.DeleteBehavior is DeleteBehavior.Cascade or DeleteBehavior.ClientCascade
                        && doCascadeDelete)
                    {
                        var cascadeState = principalIsDetached
                            || dependent.EntityState == EntityState.Added
                                ? EntityState.Detached
                                : EntityState.Deleted;

                        if (SensitiveLoggingEnabled)

Review comment (suggestion):
@copilot If that's the case then change the fix here and in EntityGraphAttacher‎ to check .SharedIdentityEntry

Code after:
        // marked Deleted and a new entry with the same key is linked via SharedIdentityEntry.
        // Skip cascade from the old entry since the replacement handles its own dependents.
        if (entry.SharedIdentityEntry != null)
        {
            return;
        }

        var doCascadeDelete = force || CascadeDeleteTiming != CascadeTiming.Never;
        var principalIsDetached = entry.EntityState == EntityState.Detached;

        var detectChangesEnabled = Context.ChangeTracker.AutoDetectChangesEnabled
            && !((IRuntimeModel)Model).SkipDetectChanges;

        foreignKeys ??= entry.EntityType.GetReferencingForeignKeys();
        foreach (var fk in foreignKeys)
        {
            if (fk.DeleteBehavior == DeleteBehavior.ClientNoAction)
            {
                continue;
            }

            foreach (InternalEntityEntry dependent in (GetDependentsFromNavigation(entry, fk)
                         ?? GetDependents(entry, fk)).ToList())
            {
                if (dependent.SharedIdentityEntry == entry)
                {
                    continue;
                }

                if (detectChangesEnabled)
                {
                    ChangeDetector.DetectChanges(dependent);
                }

                if (dependent.EntityState != EntityState.Deleted
                    && dependent.EntityState != EntityState.Detached
                    && (dependent.EntityState == EntityState.Added
                        || KeysEqual(entry, fk, dependent)))
                {
                    if (fk.DeleteBehavior is DeleteBehavior.Cascade or DeleteBehavior.ClientCascade
                        && doCascadeDelete)
                    {
                        var cascadeState = principalIsDetached
                            || dependent.EntityState == EntityState.Added
                                ? EntityState.Detached
                                : EntityState.Deleted;

                        if (SensitiveLoggingEnabled)
                        {
                            UpdateLogger.CascadeDeleteSensitive(dependent, entry, cascadeState);
                        }