Repository navigation
Pragmas
db.Pragmas gives you typed access to the SQLite pragmas the framework cares about. Read or write them as properties. No string SQL needed.
db.Pragmas.ForeignKeys = true;
db.Pragmas.JournalMode = "WAL";
db.Pragmas.SynchronousMode = SQLiteSynchronousMode.Normal;
db.Pragmas.CacheSize = -4000;
db.Pragmas.UserVersion = 3;
bool enforced = db.Pragmas.ForeignKeys;
string mode = db.Pragmas.JournalMode;
long pageSize = db.Pragmas.PageSize;
long freePages = db.Pragmas.FreelistCount;The full set of built-in accessors:
| Property | Pragma | Notes |
|---|---|---|
ForeignKeys |
foreign_keys |
True or false. Off by default in SQLite. |
JournalMode |
journal_mode |
DELETE, WAL, MEMORY, TRUNCATE, PERSIST, OFF. |
CacheSize |
cache_size |
Number of pages, or kibibytes if you pass a negative number. |
SynchronousMode |
synchronous |
An SQLiteSynchronousMode enum: Off, Normal, Full, Extra. |
UserVersion |
user_version |
An integer your app picks. |
PageSize |
page_size |
Read only after the file has been written. |
FreelistCount |
freelist_count |
Read only. |
RecursiveTriggers |
recursive_triggers |
True or false. |
TempStore |
temp_store |
0 default, 1 file, 2 memory. |
SecureDelete |
secure_delete |
True or false. |
db.Pragmas.Master and db.Pragmas.Sequence are LINQ-queryable views over sqlite_master and sqlite_sequence.
List<string> tables = db.Pragmas.Master
.Where(m => m.Type == "table")
.Select(m => m.Name)
.ToList();sqlite_sequence only exists after the first [AutoIncrement] table is created.
db.Pragmas.TableInfo(name), IndexList(name), and ForeignKeyList(name) wrap the SQLite pragma TVFs and return IQueryable<T>. The argument can be a column from an outer query, in which case the framework emits a single correlated SQL statement.
var rows = (
from m in db.Pragmas.Master
from p in db.Pragmas.TableInfo(m.Name)
where m.Type == "table" && !m.Name.StartsWith("sqlite_")
select new { Table = m.Name, Column = p.Name, Type = p.Type }
).ToList();Make a class that inherits SQLitePragmas. Then register it on the options builder:
public sealed class AppPragmas : SQLitePragmas
{
public AppPragmas(SQLiteDatabase database) : base(database) { }
public int BusyTimeoutMs
{
get => Database.ExecuteScalar<int>("PRAGMA busy_timeout");
set => Database.Execute($"PRAGMA busy_timeout = {value}");
}
}
SQLiteOptions options = new SQLiteOptionsBuilder("app.db")
.UsePragmas(db => new AppPragmas(db))
.Build();
using SQLiteDatabase db = new(options);
((AppPragmas)db.Pragmas).BusyTimeoutMs = 5000;The built-in accessors still work. ForeignKeys, JournalMode, and the others come along for free.
Pragmas affect the open connection, not the database file. If you do not use SQLiteOptions.IsWalMode (which sets WAL when the connection opens), you may need to set them again on each new connection.
Some pragmas like journal_mode and secure_delete return a row when you set them. The setter reads that row for you, so you do not need to.