dotConnect for MongoDB Documentation
On this page
    Entity Framework Core Data Type Mapping
    On this page

    Database-First Mapping

    The following table shows the property types that the Scaffold-DbContext command generates for BSON types. These types match the types that a data reader returns for the same connection and depend on the Decimal128 Mapping, Guid Format, and DateTimeOffset Format connection string parameters.

    BSON Type

    SQL Engine Column Type

    .NET Property Type

    ObjectIdOIDstring
    DoubleDOUBLEdouble
    StringVARCHARstring
    32-bit integerINTint
    32-bit integer or Double in a column declared as TINYINT, SMALLINT, or SINGLETINYINT, SMALLINT, SINGLEbyte, short, float
    64-bit integerINT64long
    Decimal128DECIMAL128Depends on Decimal128 Mapping: string (default), decimal, or double
    BooleanBOOLEANbool
    DateDATETIMEDateTime
    Binary data, subtype 4 (or 3, depending on Guid Format)UUIDGuid
    Other binary dataBLOBbyte[]
    Date with offset, stored according to DateTimeOffset FormatDATETIMEOFFSETDateTimeOffset
    TimestampBSONTIMESTAMPlong: seconds in the high 32 bits, increment in the low 32 bits
    Regular expression, JavaScriptREGEX, JAVASCRIPTstring with the value in Extended JSON
    Embedded document-Owned type (OwnsOne, ToJson)
    Array of documents-Collection of owned types (OwnsMany, ToJson)
    Array of values of one type-List<T>

    Properties for fields that can be missing or null in documents are nullable, for example, int? or DateTime?.

    Code-First Mapping

    The following table shows how .NET property types are stored in BSON when you use the Code-First approach.

    .NET Property Type

    BSON Type

    stringString; a single key (_id) is an ObjectId generated on insert, as are the foreign keys that reference it
    int32-bit integer
    short, byte32-bit integer; EnsureCreated declares the column as SMALLINT or TINYINT
    long64-bit integer
    doubleDouble
    floatDouble; EnsureCreated declares the column as SINGLE
    decimalDecimal128
    boolBoolean
    DateTimeDate; Kind depends on DateTime Kind
    DateTimeOffsetDepends on DateTimeOffset Format
    TimeSpan64-bit integer (ticks)
    GuidBinary data, subtype 4 (or as specified by Guid Format)
    enumNumeric value of the enum
    List<T>, T[], IList<T> of the types aboveArray of values (Entity Framework Core 8–11)
    Owned type, complex property in Entity Framework Core 10 and 11Embedded document
    Collection of owned typesArray of embedded documents