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 |
|---|---|---|
| ObjectId | OID | string |
| Double | DOUBLE | double |
| String | VARCHAR | string |
| 32-bit integer | INT | int |
| 32-bit integer or Double in a column declared as TINYINT, SMALLINT, or SINGLE | TINYINT, SMALLINT, SINGLE | byte, short, float |
| 64-bit integer | INT64 | long |
| Decimal128 | DECIMAL128 | Depends on Decimal128 Mapping: string (default), decimal, or double |
| Boolean | BOOLEAN | bool |
| Date | DATETIME | DateTime |
| Binary data, subtype 4 (or 3, depending on Guid Format) | UUID | Guid |
| Other binary data | BLOB | byte[] |
| Date with offset, stored according to DateTimeOffset Format | DATETIMEOFFSET | DateTimeOffset |
| Timestamp | BSONTIMESTAMP | long: seconds in the high 32 bits, increment in the low 32 bits |
| Regular expression, JavaScript | REGEX, JAVASCRIPT | string 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?.
The following table shows how .NET property types are stored in BSON when you use the Code-First approach.
.NET Property Type | BSON Type |
|---|---|
| string | String; a single key (_id) is an ObjectId generated on insert, as are the foreign keys that reference it |
| int | 32-bit integer |
| short, byte | 32-bit integer; EnsureCreated declares the column as SMALLINT or TINYINT |
| long | 64-bit integer |
| double | Double |
| float | Double; EnsureCreated declares the column as SINGLE |
| decimal | Decimal128 |
| bool | Boolean |
| DateTime | Date; Kind depends on DateTime Kind |
| DateTimeOffset | Depends on DateTimeOffset Format |
| TimeSpan | 64-bit integer (ticks) |
| Guid | Binary data, subtype 4 (or as specified by Guid Format) |
| enum | Numeric value of the enum |
| List<T>, T[], IList<T> of the types above | Array of values (Entity Framework Core 8–11) |
| Owned type, complex property in Entity Framework Core 10 and 11 | Embedded document |
| Collection of owned types | Array of embedded documents |