{"record":{"id":"181c1f7c11c66518","repo":"tursodatabase/turso","slug":"connection-must-be-a-sqliteconnection","errorCode":null,"errorMessage":"Connection must be a SqliteConnection.","messagePattern":"Connection must be a SqliteConnection\\.","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"bindings/dotnet/src/Turso.Data.Sqlite/SqliteCommand.cs","lineNumber":121,"sourceCode":"    }\n\n    public new SqliteParameterCollection Parameters { get; } = new();\n\n    public new SqliteTransaction? Transaction\n    {\n        get => _transaction;\n        set\n        {\n            ThrowIfReaderOpen(nameof(Transaction));\n            _transaction = value;\n        }\n    }\n\n    protected override DbConnection? DbConnection\n    {\n        get => Connection;\n        set => Connection = value as SqliteConnection\n                            ?? (value is null ? null : throw new ArgumentException(\"Connection must be a SqliteConnection.\", nameof(value)));\n    }\n\n    protected override DbParameterCollection DbParameterCollection => Parameters;\n\n    protected override DbTransaction? DbTransaction\n    {\n        get => Transaction;\n        set => Transaction = value as SqliteTransaction\n                            ?? (value is null ? null : throw new ArgumentException(\"Transaction must be a SqliteTransaction.\", nameof(value)));\n    }\n\n    public override void Cancel()\n    {\n        _activeManagedCommand?.Cancel();\n    }\n\n    public override int ExecuteNonQuery()\n    {","sourceCodeStart":103,"sourceCodeEnd":139,"githubUrl":"https://github.com/tursodatabase/turso/blob/6c7252267988c76e632af00a671e4b9788dfae13/bindings/dotnet/src/Turso.Data.Sqlite/SqliteCommand.cs#L103-L139","documentation":"The protected DbConnection setter (used whenever a host assigns DbCommand.Connection with a non-SqliteConnection) rejects other provider connections with ArgumentException, because the command's native statement handles only work against a Turso SqliteConnection - execution later dereferences Connection! without further checks (PrepareSingleStatement). Null clears the connection.","triggerScenarios":"Assigning a foreign DbConnection (e.g., SqlConnection) to a SqliteCommand through the base Connection property; generic DALs that store connections as DbConnection and rebind commands; ORM plumbing that reassigns connections.","commonSituations":"Provider-agnostic data layers rebinding commands at runtime; copy-pasted SQL Server data-access code; connection abstractions/pools returning a different concrete type than expected.","solutions":["Assign only the SqliteConnection the command will execute on.","Create commands via sqliteConn.CreateCommand() so the concrete type is always correct.","Pattern-match before assigning through the base property: only accept SqliteConnection."],"exampleFix":"// before\nDbCommand cmd = new SqliteCommand();\ncmd.Connection = new SqlConnection(cs); // routed to DbConnection setter -> throws\n\n// after\nusing var conn = new SqliteConnection(cs);\nDbCommand cmd = conn.CreateCommand();   // typed SqliteCommand, already bound","handlingStrategy":"type-guard","validationCode":"if (connection is not null and not SqliteConnection)\n    throw new ArgumentException(\"Connection must be a SqliteConnection.\");\n\ncmd.Connection = connection as SqliteConnection;","typeGuard":"static SqliteConnection? AsSqlite(DbConnection? c) => c switch {\n    null => null,\n    SqliteConnection s => s,\n    _ => throw new ArgumentException(\"Connection must be a SqliteConnection.\")\n};","tryCatchPattern":null,"preventionTips":["Create commands with conn.CreateCommand() on the SqliteConnection itself.","Keep connection+command provider pairs together in generic DALs.","Never rebind a SqliteCommand to another provider's connection."],"tags":["dotnet","ado-net","connection","type-mismatch"],"backgroundTag":"invalid-connection-type","analyzedSha":"6c7252267988c76e632af00a671e4b9788dfae13","analyzedAt":"2026-08-20T07:02:18.389Z","contentChangedAt":"2026-08-20T07:02:18.389Z","schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}