tursodatabase/turso · error · ArgumentException
Connection must be a TursoConnection.
Error message
Connection must be a TursoConnection.
What it means
The DbConnection setter on TursoCommand only accepts TursoConnection instances (TursoCommand.cs:62-73) because the command must reach the native database handle owned by that concrete type. Assigning any other DbConnection-derived object (SqlConnection, SQLiteConnection, NpgsqlConnection) throws ArgumentException; assigning null clears the connection without error.
Source
Thrown at bindings/dotnet/src/Turso.Data/TursoCommand.cs:74
}
}
public override bool DesignTimeVisible { get; set; }
public override UpdateRowSource UpdatedRowSource { get; set; }
protected override DbConnection? DbConnection
{
get => _connection;
set
{
if (value is null)
{
_connection = null;
return;
}
_connection = value as TursoConnection
?? throw new ArgumentException("Connection must be a TursoConnection.", nameof(value));
_commandTimeout = _connection.DefaultTimeout;
}
}
protected override DbParameterCollection DbParameterCollection => _parameterCollection;
public new virtual TursoParameterCollection Parameters => _parameterCollection;
protected override DbTransaction? DbTransaction
{
get => _transaction;
set
{
if (value is null)
{
_transaction = null;
return;View on GitHub (pinned to c1e5928725)
Solutions
- Create commands from the Turso connection itself: using var cmd = tursoConn.CreateCommand();
- Or construct with the typed overload: new TursoCommand(sql, tursoConn); assigning null first if rebinding is needed.
- Fix the DI/provider registration so the same provider (TursoFactory) supplies both the connection and the commands.
Example fix
// before var cmd = new TursoCommand(); cmd.Connection = sqliteConnection; // throws ArgumentException // after var cmd = sqliteTursoConn.CreateCommand(); // or: new TursoCommand(sql, tursoConn)
Defensive patterns
Strategy: type-guard
Validate before calling
if (cmd.Connection is not null && cmd.Connection is not TursoConnection)
throw new InvalidOperationException("Command is bound to a foreign provider's connection."); Type guard
static bool IsTursoConnection(DbConnection? c) => c is null || c is TursoConnection;
Try / catch
try
{
cmd.Connection = incomingConnection;
}
catch (ArgumentException ex) when (ex.ParamName == "value")
{
// incomingConnection is from another provider; re-create the command on the Turso connection.
} Prevention
- Always create commands with tursoConn.CreateCommand() rather than newing them.
- Use the new TursoCommand(sql, conn) constructor so the binding is explicit.
- Register one provider (TursoFactory) in DI for the whole stack.
When it happens
Trigger: cmd.Connection = new SqlConnection(...); or new TursoCommand { Connection = foreignConnection } where the connection was created by a different ADO.NET provider.
Common situations: Copy-paste migration from Microsoft.Data.SqlClient where only the class names changed; provider-agnostic DALs that pass DbConnection around; DI container registered with the wrong provider so a foreign connection is injected into code that creates TursoCommands.
Related errors
- Batch command must be a TursoBatchCommand.
- Transaction must be a TursoTransaction.
- Parameter must be of type TursoParameter
- Connection must be set before executing a command.
- Connection must be set before preparing a command.
AI-assisted analysis of tursodatabase/turso@c1e5928725 (2026-08-20).
Data as JSON: /api/errors/4ad9f5025b10309e.
Report an issue: GitHub.