{"record":{"id":"cdd9aaad19c72f55","repo":"tursodatabase/turso","slug":"the-connection-string-can-only-be-set-when-the-con","errorCode":null,"errorMessage":"The connection string can only be set when the connection is closed.","messagePattern":"The connection string can only be set when the connection is closed\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"bindings/dotnet/src/Turso.Data.Sqlite/SqliteConnection.cs","lineNumber":69,"sourceCode":"                \"Only direct remote and embedded replica Turso connections can be wrapped.\",\n                nameof(connection));\n        }\n\n        _connectionOptions = options;\n        _managedConnection = connection;\n        _ownsManagedConnection = ownsConnection;\n        _wrapsManagedConnection = true;\n        _readOnly = options.Mode == SqliteOpenMode.ReadOnly;\n    }\n\n    [AllowNull]\n    public override string ConnectionString\n    {\n        get => _connectionOptions.ConnectionString;\n        set\n        {\n            if (State == ConnectionState.Open)\n                throw new InvalidOperationException(Properties.Resources.ConnectionStringRequiresClosedConnection);\n\n            var options = new SqliteConnectionStringBuilder(value);\n            if (_wrapsManagedConnection && options.IsLocal)\n            {\n                throw new InvalidOperationException(\n                    \"A SqliteConnection wrapping a TursoConnection must remain a direct remote or embedded replica connection.\");\n            }\n\n            ConfigureManagedConnection(options);\n            _connectionOptions = options;\n            _defaultTimeout = null;\n        }\n    }\n\n    public override string Database => _managedConnection?.Database ?? \"main\";\n\n    public override string DataSource => _managedConnection?.DataSource ?? _dataSource ?? _connectionOptions.DataSource;\n","sourceCodeStart":51,"sourceCodeEnd":87,"githubUrl":"https://github.com/tursodatabase/turso/blob/6c7252267988c76e632af00a671e4b9788dfae13/bindings/dotnet/src/Turso.Data.Sqlite/SqliteConnection.cs#L51-L87","documentation":"Assigning SqliteConnection.ConnectionString throws InvalidOperationException when State is Open, because the options encoded in the string (data source, mode, pragmas) have already been applied to the live database handle and cannot be swapped underneath it. The setter only accepts a new string on a closed connection. This mirrors Microsoft.Data.Sqlite behavior and prevents silently diverging connection state.","triggerScenarios":"'conn.Open(); conn.ConnectionString = newCs;' in code that reconfigures connections in place; config-reload logic that pushes a fresh connection string into an existing open connection; helper factories that set properties on a shared, already-open connection object.","commonSituations":"DI singletons that get restarted settings without recreating the connection, hot-reload of appsettings while pooled/open connections exist, and test suites mutating one connection across test cases.","solutions":["Close the connection before assigning a new ConnectionString, then reopen.","Prefer constructing a new SqliteConnection with the new string instead of mutating an existing one.","Set the connection string once at construction ('new SqliteConnection(cs)') and treat it as immutable."],"exampleFix":"// before\nvar conn = new SqliteConnection(cs1);\nconn.Open();\nconn.ConnectionString = cs2; // throws: can only be set when closed\n\n// after\nvar conn = new SqliteConnection(cs1);\nconn.Open();\nconn.Close();\nconn.ConnectionString = cs2;\nconn.Open();","handlingStrategy":"validation","validationCode":"static void SafeSetConnectionString(SqliteConnection conn, string cs)\n{\n    if (conn.State != ConnectionState.Closed)\n        conn.Close();\n    conn.ConnectionString = cs;\n}","typeGuard":null,"tryCatchPattern":"null","preventionTips":["Treat ConnectionString as construct-time configuration: 'new SqliteConnection(cs)'.","On config reload, build new connection objects instead of mutating live ones.","Add a debug assertion that ConnectionString is only assigned when State == Closed."],"tags":["ado-net","sqlite","turso","connection-string","connection-lifecycle"],"backgroundTag":"invalid-connection-state","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"}