Skip to content

Implement events for before and after SaveChanges #15910

Description

@ajcvickers

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

  1. added this to the 3.0.0 milestone on Jun 3, 2019
  2. self-assigned this
    on Jun 3, 2019
  3. 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)

  4. 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.

  5. douglasg14b commented on Jun 16, 2019

    @douglasg14b

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

  6. modified the milestones: 3.0.0, Backlog on Jun 28, 2019
  7. SidShaytay commented on Jul 23, 2019

    @SidShaytay

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

  8. ajcvickers commented on Jul 25, 2019

    @ajcvickers
    ContributorAuthor

    @SidShetye We haven't decided what will be in 3.1 yet.

  9. added
    good first issue
    on Sep 2, 2019
  10. 30 remaining items

  11. added and removed
    good first issue
    on Jul 30, 2020
  12. added a commit that references this issue on Jul 30, 2020
    eaefa43
  13. modified the milestones: 5.0.0, on Aug 14, 2020
  14. modified the milestones: , 5.0.0 on Nov 7, 2020
  15. removed their assignment
    on Sep 1, 2024
  16. 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

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions