tursodatabase/turso · error · InvalidOperationException

Connection must be set before executing a batch.

Error message

Connection must be set before executing a batch.

What it means

SqliteBatch.ValidateBatch requires Connection to be assigned before execution; executing a batch with a null Connection throws InvalidOperationException. This is the ADO.NET 'CallRequiresOpenConnection' style precondition, checked first in ValidateBatch.

Source

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

            if (managedCommands.Count == 0)
            {
                managedBatch.Dispose();
                return new BatchState(null, statements, managedCommands, mappings);
            }

            return new BatchState(managedBatch, statements, managedCommands, mappings);
        }
        catch
        {
            managedBatch.Dispose();
            throw;
        }
    }

    private SqliteConnection ValidateBatch()
    {
        var connection = _connection
                         ?? throw new InvalidOperationException(
                             "Connection must be set before executing a batch.");
        if (!connection.IsManagedConnection)
        {
            throw new NotSupportedException(
                "SQLite facade batches are available only for direct remote or embedded replica connections.");
        }
        if (connection.State != ConnectionState.Open)
            throw new InvalidOperationException(Properties.Resources.CallRequiresOpenConnection("ExecuteBatch"));
        if (_transaction is { IsCompleted: true })
            throw new InvalidOperationException(Properties.Resources.TransactionCompleted);
        if (_transaction is not null && !ReferenceEquals(_transaction.Connection, connection))
            throw new InvalidOperationException(Properties.Resources.TransactionConnectionMismatch);
        if (connection.Transaction is not null
            && !ReferenceEquals(_transaction, connection.Transaction))
        {
            throw new InvalidOperationException(Properties.Resources.TransactionRequired);
        }
        if (_batchCommands.Count == 0)

View on GitHub (pinned to 6c72522679)

Solutions

  1. Set batch.Connection = connection (a Turso SqliteConnection) before executing
  2. Use a constructor/factory overload that takes the connection, mirroring SqliteCommand(connection, transaction) patterns
  3. Assert connection is set in your data-access helper before calling Execute*

Example fix

// before
var batch = new SqliteBatch();
await batch.ExecuteReaderAsync(); // InvalidOperationException
// after
var batch = new SqliteBatch();
batch.Connection = connection;
await batch.ExecuteReaderAsync();
Defensive patterns

Strategy: validation

Validate before calling

if (batch.Connection is null)
    throw new InvalidOperationException("Assign batch.Connection before executing");

Type guard

static bool IsReady(SqliteBatch b) => b.Connection is Turso.Data.Sqlite.SqliteConnection;

Try / catch

try { await batch.ExecuteReaderAsync(); }
catch (InvalidOperationException ex) when (ex.Message.Contains("Connection must be set")) { /* fix batch construction */ }

Prevention

When it happens

Trigger: Calling ExecuteReader/ExecuteDbDataReaderAsync (or ExecuteBatch) on a freshly constructed SqliteBatch without setting its Connection property, or after setting it to null.

Common situations: Forgot to pass the connection in a factory/helper that builds the batch; batch created via DI with unset property; reusing a batch whose connection was cleared.

Understand the failure class

Background: "missing required argument" and "the following required arguments were not provided": what required-argument errors mean and how to fix them — this error's family across 20 libraries.

Related errors


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