Platform · Files
The database as a file
IBsqliteStorageAdmin treats a database as an artifact rather than a
connection: what exists, read the bytes out, write bytes back, delete. Cross-provider
migration and the diagnostics on Storage are built from this same
surface, and none of it goes through EF.
Fastest persistent tier. Real files in the origin private file system. Concurrent connections, no COOP/COEP.
What each backend holds
Names come from the provider, so a backend that has never been used reports empty
rather than failing, and one this browser cannot use says why. Related
-journal files are not databases and do not appear.
// Databases as files, outside any SQL connection. No EF involved.
foreach (var provider in Database.Providers)
{
// A backend this browser cannot use has nothing to list; probe.UnavailableReason says why.
var probe = await provider.ProbeAsync();
if (!probe.IsAvailable)
{
continue;
}
IReadOnlyList<string> names = await provider.Admin.ListAsync();
}Download a real .db
Snapshots app.db while it stays open, as of its last
committed write - a transaction another context has open is waited for, not captured
half done. On OPFS the image is the file byte for byte; on IndexedDB and Cache Storage it
is a compacted copy made with VACUUM INTO. Either way it is an ordinary
SQLite file, and a backup RestoreAsync takes back.
@inject BsqliteDatabase Database
// A snapshot of the open database, as of its last committed write.
byte[] image = await Database.ExportAsync();
// A plain SQLite file: open it in any desktop tool, or restore it later.
await js.InvokeVoidAsync("bsqliteDemo.download", "app.db", image);Restore from a file
Replaces app.db with the file, on the backend that
holds it now. The file is checked before anything is touched - one that is not a SQLite
database, or is cut short, is refused and the database stays as it was - and a file a
server or desktop left in WAL mode is accepted. The database closes around the import,
is read back to check every byte landed, and reopens; the context factory then applies
the migrations an older backup is missing.
// Checked first - not a SQLite database, or cut short, and nothing changes. Then the
// database closes (Closing handlers let go), the file replaces it where it lives, is
// read back to verify, and the database reopens (Opened handlers reload).
await Database.RestoreAsync(bytes);
// A context applies the migrations an older backup is missing.
await using var db = await Contexts.CreateDbContextAsync();Start over
Removes app.db from every backend that holds a copy
and forgets the binding, so the next open is free to pick the preferred provider again,
run the migrations, and reseed. Every backend, not only the current one: a copy left
elsewhere would be found again on the next open - what signing out of an offline app
must not allow.
// Closes the database and deletes it from every backend that holds a copy, then forgets
// the binding - so selection is free to pick the preferred backend again, and no copy
// left elsewhere comes back on the next open.
await Database.DeleteAsync();
// Migrates and reseeds the now-empty database.
await using var db = await Contexts.CreateDbContextAsync();