For AI agents: the complete documentation index is available at /lite-db/en/llms.txt, the full documentation bundle is available at /lite-db/en/llms-full.txt, and this page is available as Markdown at /lite-db/en/usage/data-models.md.

Data Models

A data model must be a C# class with a public parameterless constructor. LiteDB.Get<T>() and LiteDB.Query<T>() match the result column names to the model's public settable properties or public fields.

using Achieve.Database;
using SQLite;

[Table("ItemData")]
public sealed class ItemData : IDataBase
{
    [PrimaryKey]
    public int Id { get; set; }
    public string Name { get; set; }
    public int Price { get; set; }
    public float Weight { get; set; }
}

IDataBase

GetList, Exist, and TryGetValue rely on IDataBase, expecting Id and Name.

public interface IDataBase
{
    int Id { get; set; }
    string Name { get; set; }
}

Supported attributes

AttributeDescription
[Table("Name")]Specifies a table name different from the class name
[Column("Name")]Specifies a column name different from the member
[PrimaryKey]Marks the primary key member
[AutoIncrement]Attribute kept for code-generation compatibility
[Ignore]Excludes a member from mapping
[NotNull]Attribute used for model representation

[AutoIncrement] and [NotNull] are not used during runtime schema generation because the package does not provide a table-creation API. They exist to keep the meaning consistent when creating a schema with the CSV Importer or an external DB tool.

Column mapping

Mapping is case-insensitive by default.

[Column("display_name")]
public string DisplayName { get; set; }

Properties without a setter and readonly fields are excluded from mapping.

Type mapping

The query API operates on the Id column. SQLite types map to C# types as follows.

SQLite typeC# type
INTEGERint, long, bool, enum
REALfloat, double
TEXTstring, DateTime, Guid
BLOBbyte[]

Recommended rules:

  • Give the table an Id column.
  • Manage Id as the primary key for models you look up by a single key.
  • Keep C# property names the same as DB column names.
  • Use [Table("TableName")] when the class name and table name differ.