Skip to content

Commit a3e087c

Browse files
author
Bart Koelman
committed
Add handling of nullability in value types to docs
1 parent 41a5505 commit a3e087c

File tree

1 file changed

+25
-2
lines changed

1 file changed

+25
-2
lines changed

docs/usage/resources/nullability.md

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,37 @@
1-
# Nullability
1+
# Nullability in resources
22

3-
When the [nullable reference types](https://docs.microsoft.com/en-us/dotnet/csharp/nullable-references) (NRT) compiler feature is enabled, it affects both ASP.NET ModelState validation and Entity Framework Core.
3+
Properties on a resource class can be declared as nullable or non-nullable. This affects both ASP.NET ModelState validation and the way Entity Framework Core generates database columns.
44

55
Note that ModelState validation is turned off by default. It can be enabled in [options](~/usage/options.md#enable-modelstate-validation).
66

7+
# Value types
8+
9+
When ModelState validation is enabled, non-nullable value types will **not** trigger a validation error when omitted in the request body.
10+
To make JsonApiDotNetCore return an error when such a property is missing on resource creation, declare it as nullable and annotate it with `[Required]`.
11+
12+
Example:
13+
14+
```c#
15+
public sealed class User : Identifiable<int>
16+
{
17+
[Attr]
18+
[Required]
19+
public bool? IsAdministrator { get; set; }
20+
}
21+
```
22+
23+
This makes EF Core generate non-nullable columns. And model errors are returned when nullable fields are omitted.
24+
25+
# Reference types
26+
27+
When the [nullable reference types](https://docs.microsoft.com/en-us/dotnet/csharp/nullable-references) (NRT) compiler feature is enabled, it affects both ASP.NET ModelState validation and Entity Framework Core.
28+
729
## NRT turned off
830

931
When NRT is turned off, use `[Required]` on required attributes and relationships. This makes EF Core generate non-nullable columns. And model errors are returned when required fields are omitted.
1032

1133
Example:
34+
1235
```c#
1336
public sealed class Label : Identifiable<int>
1437
{

0 commit comments

Comments
 (0)