tursodatabase/turso · error · ArgumentException

Batch command must be a SqliteBatchCommand.

Error message

Batch command must be a SqliteBatchCommand.

What it means

SqliteBatchCommandCollection (Add/Insert/SetBatchCommand) only accepts SqliteBatchCommand instances. Passing any other DbBatchCommand-derived type throws ArgumentException naming the offending parameter, because the collection stores strongly-typed commands that expose managed-statement state.

Source

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

    public override void Insert(int index, DbBatchCommand item)
        => _commands.Insert(index, RequireCommand(item));

    public override bool Remove(DbBatchCommand item)
        => item is SqliteBatchCommand command && _commands.Remove(command);

    public override void RemoveAt(int index)
        => _commands.RemoveAt(index);

    protected override DbBatchCommand GetBatchCommand(int index)
        => _commands[index];

    protected override void SetBatchCommand(int index, DbBatchCommand batchCommand)
        => _commands[index] = RequireCommand(batchCommand);

    private static SqliteBatchCommand RequireCommand(DbBatchCommand command)
        => command as SqliteBatchCommand
           ?? throw new ArgumentException(
               "Batch command must be a SqliteBatchCommand.",
               nameof(command));
}

View on GitHub (pinned to 6c72522679)

Solutions

  1. Construct commands with new SqliteBatchCommand() before adding to BatchCommands
  2. Cast or convert generic DbBatchCommand instances to SqliteBatchCommand at the provider boundary
  3. Use 'new' constraint or the concrete SqliteBatch type in helper methods instead of DbBatch

Example fix

// before
DbBatchCommand c = new MyBatchCommand(sql);
batch.BatchCommands.Add(c); // throws
// after
var c = new SqliteBatchCommand { CommandText = sql };
batch.BatchCommands.Add(c);
Defensive patterns

Strategy: type-guard

Validate before calling

if (command is not SqliteBatchCommand)
    throw new ArgumentException("Expected SqliteBatchCommand.", nameof(command));

Type guard

static bool IsSqliteBatchCommand(DbBatchCommand c) => c is SqliteBatchCommand;

Try / catch

try { batch.BatchCommands.Add(command); }
catch (ArgumentException ex) when (ex.ParamName == "batchCommand" || ex.Message.Contains("SqliteBatchCommand"))
{
    batch.BatchCommands.Add(new SqliteBatchCommand { CommandText = command.CommandText });
}

Prevention

When it happens

Trigger: Calling batch.BatchCommands.Add(someOtherDbBatchCommand) or SetBatchCommand with a command type from another provider (e.g. a custom DbBatchCommand subclass) that is not SqliteBatchCommand.

Common situations: Generic ADO.NET code that constructs DbBatchCommand instances and reuses them across providers; refactoring from Microsoft.Data.Sqlite batches while keeping custom command classes.

Understand the failure class

Background: "is not a compatible type" / "cannot merge" errors: when a value's type doesn't match what the library requires — this error's family across 65 libraries.

Related errors


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