Skip to content

Commit

Permalink
Merge required/optional page into entity properties
Browse files Browse the repository at this point in the history
  • Loading branch information
roji committed Dec 16, 2019
1 parent f6123f4 commit 1f75748
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 56 deletions.
5 changes: 5 additions & 0 deletions .openpublishing.redirection.json
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,11 @@
"redirect_url": "/ef/core/modeling/entity-properties#maximum-length",
"redirect_document_id": false
},
{
"source_path": "entity-framework/core/modeling/required-optional.md",
"redirect_url": "/ef/core/modeling/entity-properties#required-and-optional-properties",
"redirect_document_id": false
},
{
"source_path": "entity-framework/core/modeling/relational/columns.md",
"redirect_url": "/ef/core/modeling/entity-properties#column-names",
Expand Down
2 changes: 1 addition & 1 deletion entity-framework/core/get-started/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -160,5 +160,5 @@ Now you can run the app:

* Follow the [ASP.NET Core Tutorial](/aspnet/core/data/ef-rp/intro) to use EF Core in a web app
* Learn more about [LINQ query expressions](/dotnet/csharp/programming-guide/concepts/linq/basic-linq-query-operations)
* [Configure your model](xref:core/modeling/index) to specify things like [required](xref:core/modeling/required-optional) and [maximum length](xref:core/modeling/entity-properties#maximum-length)
* [Configure your model](xref:core/modeling/index) to specify things like [required](xref:core/modeling/entity-properties#required-and-optional-properties) and [maximum length](xref:core/modeling/entity-properties#maximum-length)
* Use [Migrations](xref:core/managing-schemas/migrations/index) to update the database schema after changing your model
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ This page introduces EF Core's support for nullable reference types, and describ

## Required and optional properties

The main documentation on required and optional properties and their interaction with nullable reference types is the [Required and Optional Properties](xref:core/modeling/required-optional) page. It is recommended you start out by reading that page first.
The main documentation on required and optional properties and their interaction with nullable reference types is the [Required and Optional Properties](xref:core/modeling/entity-properties#required-and-optional-properties) page. It is recommended you start out by reading that page first.

> [!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.
Expand Down
46 changes: 46 additions & 0 deletions entity-framework/core/modeling/entity-properties.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,3 +78,49 @@ In the following example, configuring a maximum length of 500 will cause a colum
[!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)]

***
52 changes: 0 additions & 52 deletions entity-framework/core/modeling/required-optional.md

This file was deleted.

2 changes: 0 additions & 2 deletions entity-framework/toc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,6 @@
href: core/modeling/keys.md
- name: Generated Values
href: core/modeling/generated-properties.md
- name: Required and Optional Properties
href: core/modeling/required-optional.md
- name: Concurrency Tokens
href: core/modeling/concurrency.md
- name: Shadow Properties
Expand Down

0 comments on commit 1f75748

Please sign in to comment.