{"record":{"id":"88a81c86fb51516e","repo":"tursodatabase/turso","slug":"backupdatabase-requires-an-open-connection","errorCode":null,"errorMessage":"BackupDatabase requires an open connection.","messagePattern":"BackupDatabase requires an open connection\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"bindings/dotnet/src/Turso.Data.Sqlite/SqliteConnection.cs","lineNumber":483,"sourceCode":"            throw new NotSupportedException(\"Custom extension entry points are not yet supported by the Turso SQLite-compatible provider.\");\n\n        if (_database is null)\n        {\n            _pendingExtensions.Add((file, proc));\n            return;\n        }\n\n        LoadExtensionCore(file, proc);\n    }\n\n    public virtual void BackupDatabase(SqliteConnection destination)\n        => BackupDatabase(destination, \"main\", \"main\");\n\n    public virtual void BackupDatabase(SqliteConnection destination, string destinationName, string sourceName)\n    {\n        ThrowIfManagedLocalOnly(nameof(BackupDatabase));\n        if (_database is null)\n            throw new InvalidOperationException(Properties.Resources.CallRequiresOpenConnection(\"BackupDatabase\"));\n        ArgumentNullException.ThrowIfNull(destination);\n        if (Transaction is not null)\n            throw new SqliteException(Properties.Resources.SqliteNativeError(5, \"database is locked\"), 5);\n        if (destination.State != ConnectionState.Open)\n            destination.Open();\n\n        foreach (var createSql in GetSchemaSql())\n            destination.ExecuteNonQuery(createSql);\n\n        foreach (var tableName in GetUserTableNames())\n            CopyTableRows(destination, tableName);\n    }\n\n    public new virtual SqliteCommand CreateCommand() => new(this) { Transaction = Transaction };\n\n    protected override DbCommand CreateDbCommand() => CreateCommand();\n\n    protected override DbBatch CreateDbBatch()","sourceCodeStart":465,"sourceCodeEnd":501,"githubUrl":"https://github.com/tursodatabase/turso/blob/6c7252267988c76e632af00a671e4b9788dfae13/bindings/dotnet/src/Turso.Data.Sqlite/SqliteConnection.cs#L465-L501","documentation":"This InvalidOperationException is thrown by BackupDatabase when the source SqliteConnection is not open (_database is null). Native backup requires an open connection to read pages from the source database. A preceding ThrowIfManagedLocalOnly also rejects managed-local connections.","triggerScenarios":"Calling conn.BackupDatabase(destination) (or the destinationName/sourceName overload) while conn.State is Closed, e.g. before calling Open() or after Close()/Dispose().","commonSituations":"Copy-pasted backup code that opens only the destination connection (since BackupDatabase auto-opens the destination but never the source); disposing the source earlier in a using block than the backup call.","solutions":["Call conn.Open() before invoking BackupDatabase","Verify conn.State == ConnectionState.Open prior to backup","Ensure the connection object is not disposed before the backup completes"],"exampleFix":"// before\nusing var conn = new SqliteConnection(cs);\nconn.BackupDatabase(dest); // throws: not open\n\n// after\nusing var conn = new SqliteConnection(cs);\nconn.Open();\nconn.BackupDatabase(dest);","handlingStrategy":"validation","validationCode":"if (source.State != ConnectionState.Open) source.Open();","typeGuard":"bool SourceReady(SqliteConnection c) => c.State == ConnectionState.Open;","tryCatchPattern":"try { source.BackupDatabase(dest); }\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"open connection\"))\n{\n    source.Open(); source.BackupDatabase(dest);\n}","preventionTips":["Open source connections right before backup in the same using scope"],"tags":["dotnet","backup","connection-state"],"backgroundTag":"invalid-state-transition","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"}