Repository navigation
Validate entity model and support optional one-to-one relationships on SQL Server - #2090
Merged
Merged
Conversation
bkoelman
force-pushed
the
fix-one-to-one-mssql
branch
5 times, most recently
from
September 27, 2026 20:53
a8a1ac2 to
793e2ea
Compare
Codecov Report❌ Patch coverage is Additional details and impacted files@@ Coverage Diff @@
## master #2090 +/- ##
==========================================
+ Coverage 92.03% 92.14% +0.11%
==========================================
Files 364 366 +2
Lines 11898 12079 +181
Branches 1995 2032 +37
==========================================
+ Hits 10950 11130 +180
+ Misses 586 585 -1
- Partials 362 364 +2 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
bkoelman
force-pushed
the
fix-one-to-one-mssql
branch
2 times, most recently
from
October 5, 2026 00:05
e03d36b to
001d6b2
Compare
bkoelman
force-pushed
the
fix-one-to-one-mssql
branch
from
October 6, 2026 04:39
1853d74 to
ea6a6be
Compare
bkoelman
marked this pull request as ready for review
October 6, 2026 04:55
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Due to limitations in SQL Server, the default delete behavior for nullable foreign keys in Entity Framework Core is
ClientSetNullinstead ofSetNull. This means that EF Core sendsUPDATEstatements to set all related foreign keys tonullbefore sending theDELETEstatement. But that only works when all related entities are loaded in the change tracker upfront.For example, consider the following model:
When sending
DELETE /orders/1, this PR ensures that all customers withFirstOrderId or LastOrderId = 1get loaded in the change tracker before deleting the order. The missing[HasOne]onFirstOrderis intentional: this also works for relationships that are not publicly exposed.All that side-loading, which only happens when
ClientSetNullis used, is of course highly inefficient. Other databases, such as PostgreSQL and MySQL, don't need this workaround. We've documented this for a long time.This PR adds validation of the Entity Framework Core mappings. It fails when
ClientSetNullis used (explicitly or implicitly) with databases other than SQL Server. It also validates that no Identifying Foreign Keys are used, which break JSON:API semantics. By default, an exception is thrown at startup, which can be downgraded to logged warnings by settingoptions.ThrowForProblematicEntityMappings = false.DapperExample was similarly updated to fully support nullable foreign keys when deleting a resource.
Note
The
ClientCascasedelete mode (also only needed for SQL Server) remains broken. Supporting that means we need to load all related entities recursively in the change tracker. It is never used by default in Entity Framework Core. For databases other than SQL Server, useCascadeinstead. When using SQL Server, send JSON:API requests to delete related resources first to avoid a foreign key constraint error.Fixes #1118.
QUALITY CHECKLIST