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

Code before:
                    Thread.Sleep(150);
                }

                _totalElapsedTime += timer.Elapsed;

                SqliteException.ThrowExceptionForRC(rc, handle);

                // It's a SELECT statement
                if (sqlite3_column_count(stmt) != 0)
                {
                    _record = new SqliteDataRecord(stmt, rc != SQLITE_DONE, _command.Connection, AddChanges);

                    return true;
                }

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

                sqlite3_reset(stmt);

                var totalChangesAfter = sqlite3_total_changes(handle);
                int 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

Review comment (suggestion):
Add comment here, explaining the issue and why where are using `sqlite3_total_changes`.

Code after:
                    Thread.Sleep(150);
                }

                _totalElapsedTime += timer.Elapsed;

                SqliteException.ThrowExceptionForRC(rc, connectionHandle);

                // It's a SELECT statement
                if (sqlite3_column_count(stmt) != 0)
                {
                    _record = new SqliteDataRecord(stmt, rc != SQLITE_DONE, _command.Connection, AddChanges);

                    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;