-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge relational and non-relational property/column docs
* Merge included-properties, max-length, relational/columns, relational/data-types and required/optional pages into a single entity-properties page. * Clean up some minor entity type things. Part of #1669
- Loading branch information
Showing
29 changed files
with
203 additions
and
207 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
--- | ||
title: Entity Properties - EF Core | ||
description: How to configure and map entity properties using Entity Framework Core | ||
author: roji | ||
ms.date: 12/10/2019 | ||
ms.assetid: e9dff604-3469-4a05-8f9e-18ac281d82a9 | ||
uid: core/modeling/entity-properties | ||
--- | ||
# Entity Properties | ||
|
||
Each entity type in your model has a set of properties, which EF Core will read and write from the database. If you're using a relational database, entity properties map to table columns. | ||
|
||
## Included and excluded properties | ||
|
||
By convention, all public properties with a getter and a setter will be included in the model. | ||
|
||
Specific properties can be excluded as follows: | ||
|
||
### [Data Annotations](#tab/data-annotations) | ||
|
||
[!code-csharp[Main](../../../samples/core/Modeling/DataAnnotations/IgnoreProperty.cs?name=IgnoreProperty&highlight=6)] | ||
|
||
### [Fluent API](#tab/fluent-api) | ||
|
||
[!code-csharp[Main](../../../samples/core/Modeling/FluentAPI/IgnoreProperty.cs?name=IgnoreProperty&highlight=3,4)] | ||
|
||
*** | ||
|
||
## Column names | ||
|
||
By convention, when using a relational database, entity properties are mapped to table columns having the same name as the property. | ||
|
||
If you prefer to configure your columns with different names, you can do so as following: | ||
|
||
### [Data Annotations](#tab/data-annotations) | ||
|
||
[!code-csharp[Main](../../../samples/core/Modeling/DataAnnotations/ColumnName.cs?Name=ColumnName&highlight=3)] | ||
|
||
### [Fluent API](#tab/fluent-api) | ||
|
||
[!code-csharp[Main](../../../samples/core/Modeling/FluentAPI/ColumnName.cs?Name=ColumnName&highlight=3-5)] | ||
|
||
*** | ||
|
||
## Column data types | ||
|
||
When using a relational database, the database provider selects a data type based on the .NET type of the property. It also takes into account other metadata, such as the configured [maximum length](#maximum-length), whether the property is part of a primary key, etc. | ||
|
||
For example, SQL Server maps `DateTime` properties to `datetime2(7)` columns, and `string` properties to `nvarchar(max)` columns (or to `nvarchar(450)` for properties that are used as a key). | ||
|
||
You can also configure your columns to specify an exact data type for a column. For example the following code configures `Url` as a non-unicode string with maximum length of `200` and `Rating` as decimal with precision of `5` and scale of `2`: | ||
|
||
### [Data Annotations](#tab/data-annotations) | ||
|
||
[!code-csharp[Main](../../../samples/core/Modeling/DataAnnotations/ColumnDataType.cs?name=ColumnDataType&highlight=4,6)] | ||
|
||
### [Fluent API](#tab/fluent-api) | ||
|
||
[!code-csharp[Main](../../../samples/core/Modeling/FluentAPI/ColumnDataType.cs?name=ColumnDataType&highlight=5-6)] | ||
|
||
*** | ||
|
||
### Maximum length | ||
|
||
Configuring a maximum length provides a hint to the database provider about the appropriate column data type to choose for a given property. Maximum length only applies to array data types, such as `string` and `byte[]`. | ||
|
||
> [!NOTE] | ||
> Entity Framework does not do any validation of maximum length before passing data to the provider. It is up to the provider or data store to validate if appropriate. For example, when targeting SQL Server, exceeding the maximum length will result in an exception as the data type of the underlying column will not allow excess data to be stored. | ||
In the following example, configuring a maximum length of 500 will cause a column of type `nvarchar(500)` to be created on SQL Server: | ||
|
||
#### [Data Annotations](#tab/data-annotations) | ||
|
||
[!code-csharp[Main](../../../samples/core/Modeling/DataAnnotations/MaxLength.cs?name=MaxLength&highlight=4)] | ||
|
||
#### [Fluent API](#tab/fluent-api) | ||
|
||
[!code-csharp[Main](../../../samples/core/Modeling/FluentAPI/MaxLength.cs?name=MaxLength&highlight=3-5)] | ||
|
||
*** | ||
|
||
## Required and optional properties | ||
|
||
A property is considered optional if it is valid for it to contain `null`. If `null` is not a valid value to be assigned to a property then it is considered to be a required property. When mapping to a relational database schema, required properties are created as non-nullable columns, and optional properties are created as nullable columns. | ||
|
||
### Conventions | ||
|
||
By convention, a property whose .NET type can contain null will be configured as optional, whereas properties whose .NET type cannot contain null will be configured as required. For example, all properties with .NET value types (`int`, `decimal`, `bool`, etc.) are configured as required, and all properties with nullable .NET value types (`int?`, `decimal?`, `bool?`, etc.) are configured as optional. | ||
|
||
C# 8 introduced a new feature called [nullable reference types](/dotnet/csharp/tutorials/nullable-reference-types), which allows reference types to be annotated, indicating whether it is valid for them to contain null or not. This feature is disabled by default, and if enabled, it modifies EF Core's behavior in the following way: | ||
|
||
* If nullable reference types are disabled (the default), all properties with .NET reference types are configured as optional by convention (e.g. `string`). | ||
* If nullable reference types are enabled, properties will be configured based on the C# nullability of their .NET type: `string?` will be configured as optional, whereas `string` will be configured as required. | ||
|
||
The following example shows an entity type with required and optional properties, with the nullable reference feature disabled (the default) and enabled: | ||
|
||
#### [Without nullable reference types (default)](#tab/without-nrt) | ||
|
||
[!code-csharp[Main](../../../samples/core/Miscellaneous/NullableReferenceTypes/CustomerWithoutNullableReferenceTypes.cs?name=Customer&highlight=4-8)] | ||
|
||
#### [With nullable reference types](#tab/with-nrt) | ||
|
||
[!code-csharp[Main](../../../samples/core/Miscellaneous/NullableReferenceTypes/Customer.cs?name=Customer&highlight=4-6)] | ||
|
||
*** | ||
|
||
Using nullable reference types is recommended since it flows the nullability expressed in C# code to EF Core's model and to the database, and obviates the use of the Fluent API or Data Annotations to express the same concept twice. | ||
|
||
> [!NOTE] | ||
> Exercise caution when enabling nullable reference types on an existing project: reference type properties which were previously configured as optional will now be configured as required, unless they are explicitly annotated to be nullable. When managing a relational database schema, this may cause migrations to be generated which alter the database column's nullability. | ||
For more information on nullable reference types and how to use them with EF Core, [see the dedicated documentation page for this feature](xref:core/miscellaneous/nullable-reference-types). | ||
|
||
### Explicit configuration | ||
|
||
A property that would be optional by convention can be configured to be required as follows: | ||
|
||
#### [Data Annotations](#tab/data-annotations) | ||
|
||
[!code-csharp[Main](../../../samples/core/Modeling/DataAnnotations/Required.cs?name=Required&highlight=4)] | ||
|
||
#### [Fluent API](#tab/fluent-api) | ||
|
||
[!code-csharp[Main](../../../samples/core/Modeling/FluentAPI/Required.cs?name=Required&highlight=3-5)] | ||
|
||
*** |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.