{"record":{"id":"4ad9f5025b10309e","repo":"tursodatabase/turso","slug":"connection-must-be-a-tursoconnection-4ad9f5","errorCode":null,"errorMessage":"Connection must be a TursoConnection.","messagePattern":"Connection must be a TursoConnection\\.","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"bindings/dotnet/src/Turso.Data/TursoCommand.cs","lineNumber":74,"sourceCode":"        }\n    }\n\n    public override bool DesignTimeVisible { get; set; }\n    public override UpdateRowSource UpdatedRowSource { get; set; }\n\n    protected override DbConnection? DbConnection\n    {\n        get => _connection;\n        set\n        {\n            if (value is null)\n            {\n                _connection = null;\n                return;\n            }\n\n            _connection = value as TursoConnection\n                          ?? throw new ArgumentException(\"Connection must be a TursoConnection.\", nameof(value));\n            _commandTimeout = _connection.DefaultTimeout;\n        }\n    }\n\n    protected override DbParameterCollection DbParameterCollection => _parameterCollection;\n\n    public new virtual TursoParameterCollection Parameters => _parameterCollection;\n\n\n    protected override DbTransaction? DbTransaction\n    {\n        get => _transaction;\n        set\n        {\n            if (value is null)\n            {\n                _transaction = null;\n                return;","sourceCodeStart":56,"sourceCodeEnd":92,"githubUrl":"https://github.com/tursodatabase/turso/blob/c1e59287258d99b309e362a63f48822256e2f65f/bindings/dotnet/src/Turso.Data/TursoCommand.cs#L56-L92","documentation":"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.","triggerScenarios":"cmd.Connection = new SqlConnection(...); or new TursoCommand { Connection = foreignConnection } where the connection was created by a different ADO.NET provider.","commonSituations":"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.","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."],"exampleFix":"// before\nvar cmd = new TursoCommand();\ncmd.Connection = sqliteConnection; // throws ArgumentException\n\n// after\nvar cmd = sqliteTursoConn.CreateCommand(); // or: new TursoCommand(sql, tursoConn)","handlingStrategy":"type-guard","validationCode":"if (cmd.Connection is not null && cmd.Connection is not TursoConnection)\n    throw new InvalidOperationException(\"Command is bound to a foreign provider's connection.\");","typeGuard":"static bool IsTursoConnection(DbConnection? c) => c is null || c is TursoConnection;","tryCatchPattern":"try\n{\n    cmd.Connection = incomingConnection;\n}\ncatch (ArgumentException ex) when (ex.ParamName == \"value\")\n{\n    // incomingConnection is from another provider; re-create the command on the Turso connection.\n}","preventionTips":["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."],"tags":["dotnet","ado-net","tursodb","connection","type-mismatch"],"backgroundTag":"ado-net-provider-type-mismatch","analyzedSha":"c1e59287258d99b309e362a63f48822256e2f65f","analyzedAt":"2026-08-20T07:02:18.389Z","contentChangedAt":"2026-08-20T07:02:18.389Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}