You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/usage/resources/nullability.md
+25-2Lines changed: 25 additions & 2 deletions
Original file line number
Diff line number
Diff line change
@@ -1,14 +1,37 @@
1
-
# Nullability
1
+
# Nullability in resources
2
2
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.
4
4
5
5
Note that ModelState validation is turned off by default. It can be enabled in [options](~/usage/options.md#enable-modelstate-validation).
6
6
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
+
publicsealedclassUser : Identifiable<int>
16
+
{
17
+
[Attr]
18
+
[Required]
19
+
publicbool? 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
+
7
29
## NRT turned off
8
30
9
31
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.
0 commit comments