Repository navigation
Open
Description
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...
Reacted by wdhenrik, Yan Pitangui, Arad Alvand, Roman Badiornyi, Kostiantyn Nesterenko, Greg Bachraty, Paulus Pärssinen, Qian, kubik-jaroslav, AgentFire and 50 more
Activity
AgentFire commentedon Aug 25, 2023
Why would we need the entities returned from Delete to be tracked as Added instead of NotTracked?
roji commentedon Aug 25, 2023
@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.
NickStrupat commentedon Jan 3, 2025
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?
roji commentedon Jan 7, 2025
@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.
OUTPUT/RETURNINGin ExecuteUpdateAsync/ExecuteDeleteAsync #35811 as a duplicate of this issuemd-redwan-hossain commentedon Jun 9, 2025
Since ExecuteUpdate/Delete has no connection with tracking, it will be better to return the data as plain IEnumerable/ICollection
roji commentedon Jun 9, 2025
@md-redwan-hossain ExecuteUpdate/Delete could return tracked entities just like queries can return tracked entities.
md-redwan-hossain commentedon Jun 10, 2025
@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.
roji commentedon Jun 10, 2025
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.
md-redwan-hossain commentedon Jun 10, 2025
roji commentedon Jun 10, 2025
[-]Allow fetching values back with ExecuteUpdate/Delete/Insert[/-][+]Allow fetching values back with ExecuteUpdate/Delete[/+]OUTPUTSQL statement #36416 as a duplicate of this issuealexis- commentedon Feb 11, 2026
+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.
CodesInChaos commentedon Jun 4, 2026
I'd like an overload like :
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
IEnumerablevsIQueryablevsListvsIReadOnlyCollection. 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.