Repository navigation
Description
One of the drawbacks of EF has always been the ability to perform set-based deletes on a foreign key or other criteria. E.g. to perform the following SQL query operation via EF, a fetch operation needs to be run to return EF entities, which are then marked for deletion in the DbContext and then submitted to the repository as a batch. Alternatively, SQL can be executed directly against the database, or a stored procedure can be called. Each of these options is less than ideal though when working with EF.
delete
f
from
dbo.FaqCategory c
inner join
dbo.Faq f
on c.FaqCategoryId = f.FaqCategoryId
where
c.Category like '%test%'// current EF batch delete process
var query = from c in DbContext.FaqCategories
join f in DbContext.Faqs
on c.FaqCategoryId equals f.FaqCategoryId
select f;
IEnumerable<Faq> faqsToDelete = query.AsEnumerable();
DbContext.RemoveRange(faqsToDelete);
DbContext.SaveChanges();I'd like to propose proper support for set-based deletes to generate the SQL listed above (or equivalent) and send it directly to the database.
Possibly by overloading the RemoveRange method to allow for an IQueryable<TEntity> parameter to be passed in, where instead of generating a select statement, a delete statement is generated.
// possible RemoveRange overload example
public void RemoveRange<TEntity>(IQueryable<TEntity>) {...}var query = from c in DbContext.FaqCategories
join f in DbContext.Faqs
on c.FaqCategoryId equals f.FaqCategoryId
select f;
DbContext.RemoveRange(query); // or DbContext.Faqs.RemoveRange(query);
DbContext.SaveChanges();I know there are some other issues that would need to be worked out, like the unit of work scope and tracking for any existing objects that could get out of sync. Perhaps set-based operations would be single unit of work operations only, or stop tracking all entities in the context, or invalidate the entire context requiring a new instance for further operations, or return the keys for all deleted entities to locally mark them as deleted. I think having a real set-based feature would outweigh many of the potential limitations it might cause for the state of a DbContext.
If set-based delete is feasible at all, it would also be great to have a set-based update feature, although there would obviously be additional issues to work out regarding setting values.
Activity
divega commentedon Oct 1, 2015
@brent-russell thanks for submitting your proposal. We have discussed similar ideas in the past, but the idea of
RemoveRange()overload that takes a query is very interesting (Incidentally I have talked to customers in the past that simply expected things to just work this way if the argument passed toRemoveRange()was an EF query 😄).Just nitpicking on terminology, we usually refer to this feature as "set-based" (deletes or updates) as opposed to "batch" (or batching). The latter is more about the ability to pack multiple CUD statements in one round trip to the database, and we actually do have support for that in EF7.
BTW, I also agree that deletes are possibly the easiest thing to implement. We don't have plans to add this feature in the short term but I think when we decide to tackle it, we should try to deliver set-based deletes first.
Re the relationship between this and our unit of work, personally I think it would be ok if they were kept separate, e.g. the same way no-tracking queries can be executed from a
DbContextbut don't affect the state of entities in the context, set-based deletes and set-based updates would only affect the database and never have an impact on change tracking.brent-russell commentedon Oct 2, 2015
@divega yes, sorry, my language-brain-part did not translate my conceptual-brain-part information correctly. Definitely looking for a single set-based query as opposed to a SQL batch. I'll update the issue to avoid confusion.
Running it as a no-tracking operation seems great to me. In most cases it would likely be the first, only, or last operation run on those particular entities in a unit of work, so tracking wouldn't be required. It might be good to give it a different method name though to avoid confusion with the standard "remove" behavior.
[-]Proposal for Batch Delete Operation[/-][+]Proposal for Set-Based Delete Operation[/+]rowanmiller commentedon Oct 2, 2015
BTW this is already tracked by #795
divega commentedon Oct 2, 2015
Thanks. I was sure we had I but couldn't find it. Closing as dupe.
@brent-russell do you intend to contribute this?
brent-russell commentedon Oct 5, 2015
@divega I would like to, and had a look through the code this weekend, but realistically won't have time for it until probably Feb-Mar 2016.