Quick start
On this page
Use .NET 10 and SQLite to create a table, insert, query, update and delete records without a separate database service.
Install
dotnet new console -n OrmDemo -f net10.0
cd OrmDemo
dotnet add package TORM
dotnet add package TORM.SqliteTORM supplies query and mapping APIs; TORM.Sqlite supplies the SQLite driver. Keep their versions aligned.
Create and use an entity
Put this in Program.cs, then run dotnet run. It creates demo.db in the working directory.
using TORM;
await using var db = new OrmClient(new ConnectionConfig
{
DatabaseType = DatabaseType.Sqlite,
ConnectionString = "Data Source=demo.db",
CommandTimeout = 30
});
await db.CodeFirst!.InitTableAsync<Device>();
var device = new Device { Code = "P-001", Name = "Inlet pump", Enabled = true };
device.Id = await db.Insertable(device).ExecuteReturnIdentityAsync();
var saved = await db.Queryable<Device>().Where(x => x.Id == device.Id).FirstAsync();
Console.WriteLine($"Created: {saved?.Code}, {saved?.Name}");
var updated = await db.Updatable<Device>()
.SetColumns(x => new Device { Name = "Inlet pump A" })
.Where(x => x.Id == device.Id).ExecuteAsync();
var renamed = await db.Queryable<Device>().Where(x => x.Id == device.Id).FirstAsync();
Console.WriteLine($"Updated: {updated}, {renamed?.Name}");
var deleted = await db.Deletable<Device>().Where(x => x.Id == device.Id).ExecuteAsync();
var exists = await db.Queryable<Device>().Where(x => x.Id == device.Id).AnyAsync();
Console.WriteLine($"Deleted: {deleted}, exists: {exists}");
/// <summary>关系型数据库设备定义。</summary>
[OrmTable("devices")]
public sealed class Device
{
/// <summary>数据库生成的主键。</summary>
[OrmColumn(IsPrimaryKey = true, IsIdentity = true)]
public long Id { get; set; }
/// <summary>字符串业务编码。</summary>
[OrmColumn(Length = 64)]
public string Code { get; set; } = "";
/// <summary>可读设备名称。</summary>
[OrmColumn(Length = 128)]
public string Name { get; set; } = "";
/// <summary>设备启用状态。</summary>
public bool Enabled { get; set; }
}Run:
dotnet runExpected output:
Created: P-001, Inlet pump
Updated: 1, Inlet pump A
Deleted: 1, exists: FalseExecuteReturnIdentityAsync returns the generated identity, which is explicitly assigned to device.Id. Both writes use that key to limit their scope. The program deletes only its new record, leaving the table and database file. Repeating it produces the same output; identity values may differ.
If the result differs
| Symptom | What to check |
|---|---|
| SQLite provider is unregistered or fails to initialize | Confirm TORM.Sqlite is installed and its version matches core |
| Table is missing or data differs | demo.db is relative to the process working directory; inspect the actual file path |
| Query returns no record | FirstAsync() returns null for no match; this walkthrough deletes its record, while the query chapter supplies separate data |
| Connecting to an existing database | Review mapping before deciding to use CodeFirst; do not apply demonstration schema initialization directly to production |
Change databases
Install the matching provider, change DatabaseType and supply a valid connection string for that driver. SQL syntax, parameter budgets and type mapping remain provider-specific.
Continue with conditions, projections and paging. For writes that must succeed together, read transactions and connection lifetime.