Skip to content

Mise Inserts

SteffenBlake edited this page Sep 28, 2026 · 1 revision

Inserts

An INSERT command names a target, an optional column list, and one source: VALUES rows or a SELECT query. Execute it with DbWriter.

Insert one row

var command = new PostgreSqlCommandBuilder()
    .InsertInto($"{CourseTbl.Table:raw}")
    .Columns($"title, capacity, campus_id")
    .Values($"{title}, {capacity}, {campusId}");

Result<int> inserted = await writer.ExecuteAsync(command, cancellationToken);

title, capacity, and campusId are application values. Their interpolation holes become bound parameters. Column names are trusted unqualified SQL identifiers, not generated table-qualified *Col references.

The generated course_id is omitted so the database supplies it. [DatabaseGenerated], [Computed], and [ExcludeFromInsert] do not remove columns from commands automatically; choose the write list explicitly.

ExecuteAsync returns the provider's affected-row count. It does not return the generated ID. See Returned rows.

Insert several rows

var command = new PostgreSqlCommandBuilder()
    .InsertInto($"{CourseTbl.Table:raw}")
    .Columns($"title, capacity")
    .Values($"{firstTitle}, {firstCapacity}")
    .Values($"{secondTitle}, {secondCapacity}");

Each Values call adds one parenthesized row. Provide values in the column list's order. Mise does not compare the number of values with the database schema or split a large insert to satisfy the database's parameter limit.

For optional or generated values, omitting a column allows the database default to apply. Supplying null is a SQL NULL value and is not equivalent to omitting the column.

Insert from a query

var source = new PostgreSqlQueryBuilder()
    .Select($"{CourseTbl.TitleCol:raw}")
    .Select($"{CourseTbl.CapacityCol:raw}")
    .From($"{CourseTbl.Table:raw}")
    .Where($"{CourseTbl.CapacityCol:raw} >= {minimumCapacity}");

var command = new PostgreSqlCommandBuilder()
    .InsertInto($"course_archive")
    .Columns($"title, capacity")
    .FromQuery(source);

This assumes an existing course_archive table with compatible columns. The SELECT projection must match the target columns' order and types. The source compiles into the INSERT as one statement, using the same engine and parameter numbering.

Choose either Values or FromQuery; mixing them fails at compilation. The target, columns, and query source can each be assigned only once. An INSERT without either source also fails.

Transaction behavior

A writer without an enlisted or explicitly started transaction executes according to the connection provider's normal behavior. Request writer.Transaction or call BeginTransactionAsync when using Mise directly with a transaction. Partie's database Providers establish the command transaction boundary for provided writers.

See Transactions, Returned rows, and Mise in ASP.NET Core.

Clone this wiki locally