tursodatabase/turso · error · ArgumentException

Connection must be a SqliteConnection.

Error message

Connection must be a SqliteConnection.

What it means

The protected DbConnection setter (used whenever a host assigns DbCommand.Connection with a non-SqliteConnection) rejects other provider connections with ArgumentException, because the command's native statement handles only work against a Turso SqliteConnection - execution later dereferences Connection! without further checks (PrepareSingleStatement). Null clears the connection.

Source

Thrown at bindings/dotnet/src/Turso.Data.Sqlite/SqliteCommand.cs:121

    }

    public new SqliteParameterCollection Parameters { get; } = new();

    public new SqliteTransaction? Transaction
    {
        get => _transaction;
        set
        {
            ThrowIfReaderOpen(nameof(Transaction));
            _transaction = value;
        }
    }

    protected override DbConnection? DbConnection
    {
        get => Connection;
        set => Connection = value as SqliteConnection
                            ?? (value is null ? null : throw new ArgumentException("Connection must be a SqliteConnection.", nameof(value)));
    }

    protected override DbParameterCollection DbParameterCollection => Parameters;

    protected override DbTransaction? DbTransaction
    {
        get => Transaction;
        set => Transaction = value as SqliteTransaction
                            ?? (value is null ? null : throw new ArgumentException("Transaction must be a SqliteTransaction.", nameof(value)));
    }

    public override void Cancel()
    {
        _activeManagedCommand?.Cancel();
    }

    public override int ExecuteNonQuery()
    {

View on GitHub (pinned to 6c72522679)

Solutions

  1. Assign only the SqliteConnection the command will execute on.
  2. Create commands via sqliteConn.CreateCommand() so the concrete type is always correct.
  3. Pattern-match before assigning through the base property: only accept SqliteConnection.

Example fix

// before
DbCommand cmd = new SqliteCommand();
cmd.Connection = new SqlConnection(cs); // routed to DbConnection setter -> throws

// after
using var conn = new SqliteConnection(cs);
DbCommand cmd = conn.CreateCommand();   // typed SqliteCommand, already bound
Defensive patterns

Strategy: type-guard

Validate before calling

if (connection is not null and not SqliteConnection)
    throw new ArgumentException("Connection must be a SqliteConnection.");

cmd.Connection = connection as SqliteConnection;

Type guard

static SqliteConnection? AsSqlite(DbConnection? c) => c switch {
    null => null,
    SqliteConnection s => s,
    _ => throw new ArgumentException("Connection must be a SqliteConnection.")
};

Prevention

When it happens

Trigger: Assigning a foreign DbConnection (e.g., SqlConnection) to a SqliteCommand through the base Connection property; generic DALs that store connections as DbConnection and rebind commands; ORM plumbing that reassigns connections.

Common situations: Provider-agnostic data layers rebinding commands at runtime; copy-pasted SQL Server data-access code; connection abstractions/pools returning a different concrete type than expected.

Related errors


AI-assisted analysis of tursodatabase/turso@6c72522679 (2026-08-20). Data as JSON: /api/errors/181c1f7c11c66518. Report an issue: GitHub.