Skip to content

Allow fetching values back with ExecuteUpdate/Delete #29898

Description

@roji

Our current ExecuteUpdate/Delete APIs do not support returning database-generate values, although all databases support this (RETURNING or OUTPUT clause); this functionality was scoped out of 7.0 to gauge user interest.

Some initial design thoughts copied from the original issue (see Operator return values):

There is already an ExecuteMergeReturning method.

The basic versions of the Update/Delete operators should return the number of rows affected, as returned to us from DbCommand.ExecuteNonQuery.

However, PostgreSQL, SQL Server, SQLite and MySQL/MariaDB all support returning the deleted/updated data (RETURNING clause everywhere except for SQL Server, which has the OUTPUT clause).

  • We can have separate operators alongside the ones returning the bare count (DeleteAndProject? DeleteReturning?).
  • You can specify which columns you want in the RETURNING/OUTPUT clause, so we need to support projection. If no projection is given, we do RETURNING *.
  • Other than that projection, DELETE and UPDATE can't be used as a general purpose table-returning expression - they're limited. PostgreSQL supports using them in a common table expression (WITH), but not in a subquery. It's mostly for sending results back to the user.
  • So these operators would need to return IQueryable:
    • If the user composes anything other than Select, they get a translation failure.
    • This allows specifying ToArray, AsEnumerable, etc.
    • Where the database supports embedding (e.g. PostgreSQL WITH), this would allow that too.
  • If no Select is composed, we can return tracked entities just like any normal query
    • Ideally, entities returned from Delete should be tracked as Added, while those returned from Update should be Modified.
    • AsNoTracking could be used to not track, as usual.
      • Note that we previously discussed using AsNoTracking/AsTracking to distinguish between the database-only/change tracker versions (see Change-tracking bulk update/delete operations #24176); but as above, we already need these operators to indicate the tracking of the database-only output...

Activity

  1. added this to the Backlog milestone on Dec 21, 2022
  2. AgentFire commented on Aug 25, 2023

    @AgentFire

    Why would we need the entities returned from Delete to be tracked as Added instead of NotTracked?

  3. roji commented on Aug 25, 2023

    @roji
    MemberAuthor

    @AgentFire "not tracked" means you're... not tracking; if that's what you want, AsNoTracking is there for that. If you are tracking, then Added seems to be the only state that makes sense, since the entities are no longer in the database (you just deleted them).

    But this all still needs to be thought about and designed, we may decide to do something different.

  4. NickStrupat commented on Jan 3, 2025

    @NickStrupat

    Just came across this issue as I was looking for a solution to this. Are there any updates on design/discussion from the team on this?

  5. roji commented on Jan 7, 2025

    @roji
    MemberAuthor

    @NickStrupat this issue is in the backlog and has received relatively few votes (only 17), so it's unlikely to get worked on in the near future... It's also not trivial, in terms of designing the API surface.

  6. md-redwan-hossain commented on Jun 9, 2025

    @md-redwan-hossain

    Since ExecuteUpdate/Delete has no connection with tracking, it will be better to return the data as plain IEnumerable/ICollection

  7. roji commented on Jun 9, 2025

    @roji
    MemberAuthor

    @md-redwan-hossain ExecuteUpdate/Delete could return tracked entities just like queries can return tracked entities.

  8. md-redwan-hossain commented on Jun 10, 2025

    @md-redwan-hossain

    @md-redwan-hossain ExecuteUpdate/Delete could return tracked entities just like queries can return tracked entities.

    @roji yes they can return that. But having IEnumerable/ICollection as return type can be a bare minimum feature since these methods directly hits the DB without tracker.

  9. roji commented on Jun 10, 2025

    @roji
    MemberAuthor

    But having IEnumerable/ICollection as return type can be a bare minimum feature since these methods directly hits the DB without tracker.

    But non-tracking queries also hit the database without going through the change tracker, and these still return an IQueryable to allow further composition. ExecuteUpdate/ExecuteDelete are somewhat simillar to non-tracking queries in that sense - they simply also have a side-effect of changing data.

  10. md-redwan-hossain commented on Jun 10, 2025

    @md-redwan-hossain
  11. roji commented on Jun 10, 2025

    @roji
    Author
  12. added theissue type on Jun 15, 2025
  13. changed the title [-]Allow fetching values back with ExecuteUpdate/Delete/Insert[/-] [+]Allow fetching values back with ExecuteUpdate/Delete[/+] on Jul 23, 2025
  14. alexis- commented on Feb 11, 2026

    @alexis-

    +1 we use ExecuteUpdateAsync exclusively (no change tracker) across 20+ endpoints and need old column values for audit trail tracking. If we want to use EF, it currently requires a separate query before each update.

  15. CodesInChaos commented on Jun 4, 2026

    @CodesInChaos

    I'd like an overload like :

    delegate TResult UpdateProjection<TSource, TResult>(TSource oldEntity, TSource newEntity);
    
    IEnumerable<TResult> ExecuteUpdate<TSource, TResult>(
        this IQueryable<TSource> source,
        Action<UpdateSettersBuilder<TSource>> setPropertyCalls,
        Expression<UpdateProjection<TSource, TResult>> projection)
    

    This gives easy access to both the old and the new value, unlike the combination with SELECT. I do not need automatic tracking.

    I don't have strong opinions on returning IEnumerable vs IQueryable vs List vs IReadOnlyCollection. Since it executes eagerly and can't be composed, returning a collection might match those semantics best, and eagerly reading the results could free up the buffered output on the database side more quickly.

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

      No branches or pull requests

      Issue actions