Skip to content

Raise SaveChangesFailed when DetectChanges throws during SaveChanges - #39762

Merged
AndriySvyryd merged 1 commit into
dotnet:mainfrom
armin-azar:fix/39757-savechangesfailed-detectchanges
Nov 24, 2026
Merged

AndriySvyryd merged 1 commit into
dotnet:mainfrom
armin-azar:fix/39757-savechangesfailed-detectchanges

Conversation

@armin-azar

Copy link
Copy Markdown
Contributor

Fixes #39757

Problem

DbContext.SaveChanges and SaveChangesAsync call TryDetectChanges() before entering the try block whose catch handlers log SaveChangesFailed (including the interceptor hook) and raise the DbContext.SaveChangesFailed event. If DetectChanges throws, for example from an exception in a ChangeTracker.DetectingAllChanges handler or from a faulty property getter, SavingChanges and SaveChangesStarting have already run but no failure notification is raised.

Fix

Move TryDetectChanges() inside the try in both SaveChanges and SaveChangesAsync. 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 a DetectingAllChanges handler and assert that the same exception is thrown, that DbContext.SaveChangesFailed is raised with it, and that CoreEventId.SaveChangesFailed is logged.

  • With only the src change reverted: both new tests fail (2 of 2).
  • With the fix: dotnet test test/EFCore.Tests -- --filter-class "*DbContextTest" gives 276 passed, 0 failed.
  • I did not run the full suite or the provider functional tests.

Backward compatibility

No public API change. Behaviour changes only in the failure path: SaveChangesFailed handlers and interceptors are now also notified when change detection throws.

- 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

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 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 the SaveChanges and SaveChangesAsync try blocks.
  • Adds regression tests for exception propagation, DbContext.SaveChangesFailed, and CoreEventId.SaveChangesFailed logging.
  • Detection failures of type DbUpdateConcurrencyException still 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

Comment thread test/EFCore.Tests/DbContextTest.cs Resolved

@AndriySvyryd AndriySvyryd left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for your contribution!

@AndriySvyryd
AndriySvyryd merged commit c27a3a3 into dotnet:main Nov 24, 2026
20 checks passed
@dotnet-milestone-bot dotnet-milestone-bot Bot added this to the 12.0-preview1 milestone Nov 25, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

SaveChangesFailed interceptor is skipped when automatic change detection throws

3 participants