tursodatabase/turso · error · ArgumentException

Connection must be a SqliteConnection.

Error message

Connection must be a SqliteConnection.

What it means

SqliteBatch.Connection is typed as DbConnection but this facade only works with a Turso SqliteConnection. The setter downcasts the assigned value and throws ArgumentException immediately if the value is a non-null object of any other DbConnection-derived type (e.g. Microsoft.Data.Sqlite.SqliteConnection or a custom provider connection).

Source

Thrown at bindings/dotnet/src/Turso.Data.Sqlite/SqliteBatch.cs:41

        ArgumentNullException.ThrowIfNull(connection);
        _connection = connection;
        _transaction = connection.Transaction;
        _timeout = connection.DefaultTimeout;
    }

    public new SqliteBatchCommandCollection BatchCommands => _batchCommands;

    protected override DbBatchCommandCollection DbBatchCommands => _batchCommands;

    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)));
            if (_connection is not null)
                _timeout = _connection.DefaultTimeout;
        }
    }

    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)));
    }

View on GitHub (pinned to 6c72522679)

Solutions

  1. Create the connection with the Turso.Data.Sqlite namespace: new SqliteConnection(connectionString) and import that namespace, not Microsoft.Data.Sqlite
  2. Check using directives and package references: remove Microsoft.Data.Sqlite or fully qualify the Turso type
  3. If the value comes from DI, register/implement the factory so it returns Turso.Data.Sqlite.SqliteConnection

Example fix

// before
using Microsoft.Data.Sqlite;
batch.Connection = new SqliteConnection(connString); // ArgumentException
// after
using Turso.Data.Sqlite;
batch.Connection = new SqliteConnection(connString);
Defensive patterns

Strategy: type-guard

Validate before calling

if (value is not Turso.Data.Sqlite.SqliteConnection)
    throw new ArgumentException($"Expected Turso SqliteConnection, got {value?.GetType().FullName}");

Type guard

static bool IsTursoSqliteConnection(DbConnection? c) => c is Turso.Data.Sqlite.SqliteConnection;

Try / catch

try { batch.Connection = conn; }
catch (ArgumentException ex) when (ex.ParamName == "value") { /* log wrong provider type, fix DI registration */ }

Prevention

When it happens

Trigger: Assigning any DbConnection instance that is not bindings/dotnet Turso.Data.Sqlite.SqliteConnection to SqliteBatch.Connection, e.g. batch.Connection = new Microsoft.Data.Sqlite.SqliteConnection(connStr) or a connection from a DI container registered with the wrong type.

Common situations: Mixing Microsoft.Data.Sqlite and Turso.Data.Sqlite packages in one project; resolving a connection from a generic DI/factory that returns the base DbConnection type; copy-pasted code from a Microsoft.Data.Sqlite sample.

Understand the failure class

Background: "Must be a positive integer", "Invalid value", "Unsupported": the invalid-argument-value error family, when a library rejects the value you pass — this error's family across 35 libraries.

Related errors


AI-assisted analysis of tursodatabase/turso@6c72522679 (2026-09-06). Data as JSON: /api/errors/49ea57aa3aa0e2ab. Report an issue: GitHub.