dotnet / EntityFramework.Docs

Documentation for Entity Framework Core and Entity Framework 6
https://docs.microsoft.com/ef/
Creative Commons Attribution 4.0 International
1.62k stars 1.96k forks source link

Ambiguity in documentation of Shadow Property generated for required and optional one to many relationship #4543

Open AkshayAmbekar26 opened 11 months ago

AkshayAmbekar26 commented 11 months ago

There are a few things related to automatically generated shadow properties mentioned in this documentation have me confused-

First, Question for this section-

  1. I don't see any nullable type being used in this example. Is this statement correct-

    An important point to note here is that C# nullable reference types are being used

  2. 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?

    An important point to note here is that C# nullable reference types are being used, so the nullability of the reference navigation is used to determine whether or not the foreign key property is nullable, and therefore whether the relationship is optional or required.

    • These 2 statements from the next section prove that whether reference navigation is nullable or non-nullable, shadow foreign key property will be nullable by default, disproving the above statement-

      When C# nullable reference types are not being used, then the foreign key property will also, by default, be created as nullable. This means relationships with automatically created shadow properties are optional by default.

    this time the foreign key property is created as nullable because C# nullable reference types are being used and the navigation on the dependent entity type is nullable. This makes the relationship optional.

  3. If it's true that the foreign key will be nullable when reference navigation is non-nullable, then how can this be true-

    EF therefore creates a shadow foreign key property called BlogId of type int.

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?

ajcvickers commented 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:

With NRTs enabled

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

Without NRTs enabled

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
AkshayAmbekar26 commented 11 months ago

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.

ajcvickers commented 11 months ago

Re-opening to consider adding the table to the docs.