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

Paragraph for optional relationship without dependent loaded makes no sense #4502

Open CasperWSchmidt opened 12 months ago

CasperWSchmidt commented 12 months ago

Hi

According to the last table on the page, ClientSetNull throws an exception, but according to the text right below, ClientSetNull is the default. How does it make sense to have a default value that results in exceptions being thrown? I don't know if the table or the text is wrong, but surely the default results in the FK to be set to null somehow 💯

Also the notes disagree with itself:

Notes:

  • Severing a relationship is not valid here since the dependents/children are not loaded.
  • The default for optional relationships like this is ClientSetNull.
  • Dependents/children must be loaded to avoid a database exception unless the database has been configured to cascade either deletes or nulls.

Point 1 says dependents must be loaded while point 3 says configuring cascading is okay even when dependents is not loaded?


Document Details

⚠ Do not edit this section. It is required for learn.microsoft.com ➟ GitHub issue linking.

ajcvickers commented 12 months ago

Agreed the wording could be better here. The last table is specifically what happens when the dependents are not loaded. You will get an exception in this case, unless the database is configured for cascades outside of EF Core.

CasperWSchmidt commented 12 months ago

I get that the table is specifically for not loaded but as I said, how does it make sense to default this case to ClientSetNull when this will throw an exception? I guess the default should be SetNull no?

Also please clarify how point 1 and 3 is not in conflict :)

ajcvickers commented 12 months ago

@CasperWSchmidt SetNull would be a wonderful default--but SQL Server support for it is very limited. The point here is that you must load dependents otherwise this scenario will always throw.