Open AkshayAmbekar26 opened 11 months ago
The bottom line is that if NRTs are enabled and the foreign key is not specified, then the nullability of the navigation to the principal is used.
Here's the information in table form based on behaviors in EF8:
FK CLR property | Navigation to principal | Relationship |
---|---|---|
Nullable | Nullable | Optional |
Nullable | Non-nullable | Optional with warning |
Nullable | Missing | Optional |
Non-nullable | Nullable | Required |
Non-nullable | Non-nullable | Required |
Non-nullable | Missing | Required |
Missing | Nullable | Optional |
Missing | Non-nullable | Required |
Missing | Missing | Optional |
FK CLR property | Navigation to principal | Relationship |
---|---|---|
Nullable | Nullable | Optional |
Nullable | N/A | Optional |
Nullable | Missing | Optional |
Non-nullable | Nullable | Required |
Non-nullable | N/A | Required |
Non-nullable | Missing | Required |
Missing | Nullable | Optional |
Missing | N/A | Optional |
Missing | Missing | Optional |
Thanks a ton for the detailed info. I had a completely wrong understanding of what NRTs mean. I was treating them like nullable value types.
Re-opening to consider adding the table to the docs.
There are a few things related to automatically generated shadow properties mentioned in this documentation have me confused-
First, Question for this section-
If the generated foreign key property will be nullable by default, making the relationship optional regardless of the nullability of the reference navigation property. How is it fair to say the following if the nullability of reference navigation property has no impact on shadow foreign key property?
It should create BlogId of type
int?
These 2 sections have been very confusing to understand and I feel like there is a lot of ambuity here. Can you please shed some light?