Repository: dotnet/efcore
File: src/Microsoft.Data.Sqlite.Core/SqliteDataReader.cs (C#)

Code before:
                    return true;
                }

                while (rc != SQLITE_DONE)
                {
                    rc = sqlite3_step(stmt);
                    SqliteException.ThrowExceptionForRC(rc, connectionHandle);
                }

                sqlite3_reset(stmt);

                // sqlite3_changes() returns the row count from the most recent INSERT, UPDATE, or DELETE
                // and incorrectly persists across DDL statements. Use sqlite3_total_changes() before and after
                // to calculate the actual delta for this statement, ensuring DDL statements don't add stale counts.
                var totalChangesAfter = sqlite3_total_changes(connectionHandle);
                var changes = totalChangesAfter - totalChangesBefore;
                if (changes > 0)
                {
                    AddChanges(changes);
                }
            }
            catch
            {
                sqlite3_reset(_stmtEnumerator.Current);
                _stmtEnumerator.Dispose();
                _stmtEnumerator = null;
                Dispose();

                throw;
            }
        }

        return false;
    }

    private void AddChanges(int changes)
    {
        if (_recordsAffected == -1)
        {
            _recordsAffected = changes;
        }
        else
        {
            _recordsAffected += changes;
        }
    }

    /// <summary>
    ///     Closes the data reader.
    /// </summary>
    public override void Close()

Review comment (suggestion):
@copilot Remove the whole `if` block and use just `AddChanges(changes)`. Because even if it is 0, we want to add that, so the RowsAffected` is set to 0 (initially) and it does not change the total count after.

Code after:
                    return true;
                }

                while (rc != SQLITE_DONE)
                {
                    rc = sqlite3_step(stmt);
                    SqliteException.ThrowExceptionForRC(rc, connectionHandle);
                }

                sqlite3_reset(stmt);

                // sqlite3_changes() returns the row count from the most recent INSERT, UPDATE, or DELETE
                // and incorrectly persists across DDL statements. Use sqlite3_total_changes() before and after
                // to calculate the actual delta for this statement, ensuring DDL statements don't add stale counts.
                var totalChangesAfter = sqlite3_total_changes(connectionHandle);
                var changes = totalChangesAfter - totalChangesBefore;
                // sqlite3_total_changes, unfortunately, counts also changes from triggers, etc. which is not what we want.
                // So we use it only to detect changes and if so, use sqlite3_changes.
                if (changes > 0)
                {
                    AddChanges(sqlite3_changes(connectionHandle));
                }
                else
                {
                    AddChanges(0);
                }
            }
            catch
            {
                sqlite3_reset(_stmtEnumerator.Current);
                _stmtEnumerator.Dispose();
                _stmtEnumerator = null;
                Dispose();

                throw;
            }
        }

        return false;
    }

    private void AddChanges(int changes)
    {
        if (_recordsAffected == -1)
        {
            _recordsAffected = changes;
        }
        else
        {
            _recordsAffected += changes;
        }