Skip to content

Proposal for Set-Based Delete Operation #3303

Closed as not planned
Closed as not planned

Description

@brent-russell

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

  1. divega commented on Oct 1, 2015

    @divega
    Contributor

    @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 to RemoveRange() 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 DbContext but 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.

  2. brent-russell commented on Oct 2, 2015

    @brent-russell
    Author

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

  3. changed the title [-]Proposal for Batch Delete Operation[/-] [+]Proposal for Set-Based Delete Operation[/+] on Oct 2, 2015
  4. rowanmiller commented on Oct 2, 2015

    @rowanmiller
    Contributor

    BTW this is already tracked by #795

  5. divega commented on Oct 2, 2015

    @divega
    Contributor

    Thanks. I was sure we had I but couldn't find it. Closing as dupe.

    @brent-russell do you intend to contribute this?

  6. brent-russell commented on Oct 5, 2015

    @brent-russell
    Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions