Skip to content

Mise Tables

SteffenBlake edited this page Sep 28, 2026 · 1 revision

Table mappings

A mapped table gives your queries compile-time constants for the table and its columns. Declare a static partial class with your database's table attribute. Map each static property with [Column].

Map a table

// CourseTbl.cs
[PostgreSqlTable("courses")]
[Alias("Course")]
public static partial class CourseTbl
{
    [Column("course_id")]
    [PrimaryKey]
    [DatabaseGenerated]
    private static int CourseId { get; }

    [Column("title")]
    private static string Title => string.Empty;

    [Column("capacity")]
    private static int Capacity { get; }

    [Column("campus_id")]
    [Relationship(CampusTbl.CampusIdCol)]
    private static int? CampusId { get; }
}

The table represents this database shape:

CREATE TABLE courses (
    course_id INTEGER GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
    title TEXT NOT NULL,
    capacity INTEGER NOT NULL,
    campus_id INTEGER NULL
);

CampusTbl is defined in Relationships. Mise generates CourseTbl.Table, CourseTbl.CourseIdCol, CourseTbl.TitleCol, CourseTbl.CapacityCol, and CourseTbl.CampusIdCol. Column constants include their table qualifier, such as "courses"."title".

These declarations describe existing database objects. They do not execute DDL or create the table. Declare and run your schema migrations separately.

Choose the table attribute

Database Attribute
SQL Server [SqlServerTable("courses")]
PostgreSQL [PostgreSqlTable("courses")]
SQLite [SqliteTable("courses")]
MySQL [MySqlTable("courses")]
MariaDB [MariaDbTable("courses")]

The attribute comes from the database runtime namespace. The target and containing types must be partial; the table target must also be static. Static mapping properties provide type information and names, rather than holding rows in memory.

Schemas and databases

SQL Server and PostgreSQL support [Schema("catalog")] on a mapped table. MySQL and MariaDB use [Database("catalog")]. Generated references include the qualifier and database-specific quoting.

Use generated constants in SQL fragments with :raw. Do not reconstruct quoted names yourself. See SQL interpolation.

Keys and write metadata

Attribute Meaning
[PrimaryKey] A key column at position zero
[PrimaryKey(1)] A later column in a composite key
[DatabaseGenerated] The database supplies the value on insert
[Computed] The database computes the value
[ExcludeFromInsert] Column marked for exclusion from inserts
[ExcludeFromUpdate] Column marked for exclusion from updates

Composite-key positions must be nonnegative and unique. A column cannot be both [DatabaseGenerated] and [Computed].

These attributes describe columns; the builders do not infer write column lists from them. Leave generated or excluded columns out of INSERT and UPDATE statements explicitly. See Inserts and Updates.

Virtual table names

Pass null to the table attribute when declaring a named SQL source such as a CTE:

// TreeTbl.cs
[PostgreSqlTable(null)]
public static partial class TreeTbl
{
    [Column("category_id")]
    private static int CategoryId { get; }
}

The generator infers tree from TreeTbl, adds Name, and generates ordinary table and column constants. It cuts the type name at its last Tbl when that suffix starts after the first character, then lowercases the first character. TreeTblPostgreSql also infers tree.

Use TreeTbl.Name when naming a CTE. See Common table expressions, Aliases, and Mapping diagnostics.

Clone this wiki locally