Skip to content

Implement events for before and after SaveChanges #15910

@ajcvickers

Description

@ajcvickers
Contributor

Splitting this out as a separate issue as required on #626 so we can track it independently of other life-cycle hooks.

  • SavingChanges: An event fired when the entity object is just about to be written to the database.
  • SavedChanges: An event fired after the SavingChanges operation has completed (successfully or otherwise)

Activity

added this to the 3.0.0 milestone on Jun 3, 2019
self-assigned this
on Jun 3, 2019
nphmuller

nphmuller commented on Jun 7, 2019

@nphmuller

For my main use case: A query level cache, it looks like I'd need an event after SaveChanges.
Specifically, I need to know if the transaction has been committed (succeeded) or has been rolled back.

The query level cache we used for EF6 was based on this project: https://github.com/moozzyk/EFCache
We're basically thinking of porting this to EFCore if the necessary events are supported. Of course things maybe can be simplified.

Basically EFCache is using a custom DBCommand to figure out which entity sets are affected during Save/Rollback. It only needs to invalidate every cache entry of the specific entity set, so that's all the information it needs. The invalidation is done via IDbTransactionInterceptor's RolledBack and Committed hooks.

If the new AfterSaveChanges event of EFCore supported the following this logic described above could be simplified:

  • The event needs to expose if the transaction got committed or if it got rolled back.
  • If it could expose which entity sets are affected by the transaction, much of the logic of EFCache could be simplified.

See also this comment by the author of EFCache: #5858 (comment)

ajcvickers

ajcvickers commented on Jun 7, 2019

@ajcvickers
ContributorAuthor

@nphmuller Thanks for the info. I've also been doing some experiments with database interceptors, similar to EF6, which might be a better fit for this.

douglasg14b

douglasg14b commented on Jun 16, 2019

@douglasg14b

My use case for this would be logging and audit trails.

modified the milestones: 3.0.0, Backlog on Jun 28, 2019
SidShetye

SidShetye commented on Jul 23, 2019

@SidShetye

@ajcvickers Could we please have this for 3.1 (along with it's counterpart in #15911)

41 remaining items

removed
good first issueThis issue should be relatively straightforward to fix.
on Jul 30, 2020
added a commit that references this issue on Jul 30, 2020
eaefa43
modified the milestones: 5.0.0, 5.0.0-rc1 on Aug 14, 2020
modified the milestones: 5.0.0-rc1, 5.0.0 on Nov 7, 2020
removed their assignment
on Sep 1, 2024
added theissue type on Jun 15, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Relationships

    None yet

      Development

      Participants

      @douglasg14b@ajcvickers@roji@SidShetye@nphmuller

      Issue actions

        Implement events for before and after SaveChanges · Issue #15910 · dotnet/efcore