{"record":{"id":"49768f167e77fd30","repo":"tursodatabase/turso","slug":"connection-must-be-set-before-executing-a-batch","errorCode":null,"errorMessage":"Connection must be set before executing a batch.","messagePattern":"Connection must be set before executing a batch\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"bindings/dotnet/src/Turso.Data.Sqlite/SqliteBatch.cs","lineNumber":365,"sourceCode":"            if (managedCommands.Count == 0)\n            {\n                managedBatch.Dispose();\n                return new BatchState(null, statements, managedCommands, mappings);\n            }\n\n            return new BatchState(managedBatch, statements, managedCommands, mappings);\n        }\n        catch\n        {\n            managedBatch.Dispose();\n            throw;\n        }\n    }\n\n    private SqliteConnection ValidateBatch()\n    {\n        var connection = _connection\n                         ?? throw new InvalidOperationException(\n                             \"Connection must be set before executing a batch.\");\n        if (!connection.IsManagedConnection)\n        {\n            throw new NotSupportedException(\n                \"SQLite facade batches are available only for direct remote or embedded replica connections.\");\n        }\n        if (connection.State != ConnectionState.Open)\n            throw new InvalidOperationException(Properties.Resources.CallRequiresOpenConnection(\"ExecuteBatch\"));\n        if (_transaction is { IsCompleted: true })\n            throw new InvalidOperationException(Properties.Resources.TransactionCompleted);\n        if (_transaction is not null && !ReferenceEquals(_transaction.Connection, connection))\n            throw new InvalidOperationException(Properties.Resources.TransactionConnectionMismatch);\n        if (connection.Transaction is not null\n            && !ReferenceEquals(_transaction, connection.Transaction))\n        {\n            throw new InvalidOperationException(Properties.Resources.TransactionRequired);\n        }\n        if (_batchCommands.Count == 0)","sourceCodeStart":347,"sourceCodeEnd":383,"githubUrl":"https://github.com/tursodatabase/turso/blob/6c7252267988c76e632af00a671e4b9788dfae13/bindings/dotnet/src/Turso.Data.Sqlite/SqliteBatch.cs#L347-L383","documentation":"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.","triggerScenarios":"Calling ExecuteReader/ExecuteDbDataReaderAsync (or ExecuteBatch) on a freshly constructed SqliteBatch without setting its Connection property, or after setting it to null.","commonSituations":"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.","solutions":["Set batch.Connection = connection (a Turso SqliteConnection) before executing","Use a constructor/factory overload that takes the connection, mirroring SqliteCommand(connection, transaction) patterns","Assert connection is set in your data-access helper before calling Execute*"],"exampleFix":"// before\nvar batch = new SqliteBatch();\nawait batch.ExecuteReaderAsync(); // InvalidOperationException\n// after\nvar batch = new SqliteBatch();\nbatch.Connection = connection;\nawait batch.ExecuteReaderAsync();","handlingStrategy":"validation","validationCode":"if (batch.Connection is null)\n    throw new InvalidOperationException(\"Assign batch.Connection before executing\");","typeGuard":"static bool IsReady(SqliteBatch b) => b.Connection is Turso.Data.Sqlite.SqliteConnection;","tryCatchPattern":"try { await batch.ExecuteReaderAsync(); }\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"Connection must be set\")) { /* fix batch construction */ }","preventionTips":["Use constructor/factory helpers that always set Connection","Initialize batch and connection together in a single factory method","Never construct SqliteBatch with parameterless ctor without immediately assigning Connection"],"tags":["dotnet","batch","connection","missing-argument"],"backgroundTag":"missing-required-argument","analyzedSha":"6c7252267988c76e632af00a671e4b9788dfae13","analyzedAt":"2026-09-06T07:15:26.990Z","contentChangedAt":"2026-09-06T07:15:26.990Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}