Skip to content

Primitives

Leonardo Porro edited this page Jan 25, 2023 · 15 revisions

A simple, plain schema with no members or elements, usually mapped to a single field of a database, and often represented by a .NET plain value such as string, int, bool.

The mapper copies primitives as-is without inspecting members or iterators. Target values are overwritten, there are no merge for primitives.

The list of built-in primitives can be found here.

Mapper primitives are not .NET primitives

Any clr type can be configured as a primitive, even if it has members or items. For example, string is an array of char, but for mapping purposes, it is not desired for string to be mapped char by char but just copied entirely, so that it is flagged as primitive by default.

There are other custom and/or 3rd party types that should behave as an atomic value, like string. For example, inspecting System.Data.Spatial.DbGeography from SQL Spatial library to try to map it member by member generates several runtime errors. Configuring those types as primitives will, most of the times, solve the errors.

Configuration

In order to mark a type as primitive, add it to the Primitives list when configuring the library:

optionsBuilder.UseMapping(cfg =>
{
    cfg.Default(opts =>
    {
        opts.Primitives.Add(typeof(DbGeography));
     });
});