✅ is there any way to delete items from the database and return the deleted list in one request?
is there any way to delete items from the database and return the deleted list in one request?
I can do something like this:
DbContext.T.Where(entity => ..).ExecuteDelete()
, but my business logic assumes that I will return items that were deleted
I can do this:
var deleted = DbContext.T.Where(entity => ..)
DbContext.RemoveRange(deleted)
but these are already two requests to database8 Replies
i dont think so
base on your code you have 2 queries
I don't think you can do that with EFCore
You can with linq2db
for sure
btw only do two queries if you're in a transaction
I think row sql is the best option here -
delete from x where x.a = 1 returning *
with DbContext.T.Where(entity => ..).ExecuteDelete()
I can only delete but I need to return deleted items tooit's important to understand what happens behind the scenes. If you retrieve the range ef core tracks them, so when you proceed to delete them efcore already knows where they are and what state they are in, so the deletion is already optmized. It's not a question of using less commands but a question of understanding why using 2 is fine
might as well not use EFCore...
A good option is generating a query with info from
dbContext.Model
and running a raw querythanks for helping