Repository navigation
Raise SaveChangesFailed when DetectChanges throws during SaveChanges - #39762
Merged
AndriySvyryd merged 1 commit intoNov 24, 2026
Merged
AndriySvyryd merged 1 commit into
AndriySvyryd merged 1 commit into
Conversation
- Move TryDetectChanges() inside the try block in SaveChanges and SaveChangesAsync so exceptions thrown while detecting changes reach the existing catch handlers - Add tests for both the sync and async paths Fixes dotnet#39757
Contributor
There was a problem hiding this comment.
🟡 Changes recommended
The regression test verifies only the DbContext event and logger, leaving the interceptor hook unasserted on both synchronous and asynchronous paths.
1 open issue
What changed in this PR
Moves automatic change detection into the synchronous and asynchronous SaveChanges failure-handling paths so detection exceptions can raise save-failure notifications.
Changes:
- Wraps
TryDetectChanges()in theSaveChangesandSaveChangesAsynctryblocks. - Adds regression tests for exception propagation,
DbContext.SaveChangesFailed, andCoreEventId.SaveChangesFailedlogging. - Detection failures of type
DbUpdateConcurrencyExceptionstill bypass diagnostics and interceptor notifications, and the tests do not directly verify interceptor callbacks.
| File | Description |
|---|---|
| test/EFCore.Tests/DbContextTest.cs | Adds synchronous and asynchronous regression tests for change-detection exceptions, failure events, and failure logging. |
| src/EFCore/DbContext.cs | Moves synchronous and asynchronous automatic change detection inside the existing save exception-handling paths. |
Review effort: Lite
AndriySvyryd
approved these changes
Nov 24, 2026
AndriySvyryd
left a comment
Member
There was a problem hiding this comment.
Thanks for your contribution!
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.

Fixes #39757
Problem
DbContext.SaveChangesandSaveChangesAsynccallTryDetectChanges()before entering thetryblock whosecatchhandlers logSaveChangesFailed(including the interceptor hook) and raise theDbContext.SaveChangesFailedevent. IfDetectChangesthrows, for example from an exception in aChangeTracker.DetectingAllChangeshandler or from a faulty property getter,SavingChangesandSaveChangesStartinghave already run but no failure notification is raised.Fix
Move
TryDetectChanges()inside thetryin bothSaveChangesandSaveChangesAsync. Exceptions from change detection now take the same path as any other save failure. The exception type and the rethrow behaviour are unchanged.Tests
Two new tests in
DbContextTest(sync and async). They throw from aDetectingAllChangeshandler and assert that the same exception is thrown, thatDbContext.SaveChangesFailedis raised with it, and thatCoreEventId.SaveChangesFailedis logged.dotnet test test/EFCore.Tests -- --filter-class "*DbContextTest"gives 276 passed, 0 failed.Backward compatibility
No public API change. Behaviour changes only in the failure path:
SaveChangesFailedhandlers and interceptors are now also notified when change detection throws.