EF Core Global Query Filter for SaveChanges (Update, Delete)

Open
#24,122 12 comments 13 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
5/5
Estimated time
Over a week
Newbie friendliness
25/100
Issue type
Feature
Clarity
Needs clarification
Activity status
Stale
Tech stack
csharp, sqlite
Domain
databases

Research direction

Start with EF Core 5.0 SaveChanges update and delete generation, then compare it with global query filters and the command interceptor approach described in the issue. The work would need a defined design and provider behavior for SQL Server and SQLite before implementation can be considered done.

Written by the indexing model from the issue text.

Description

area-save-changes customer-reported

Is there any way to apply a query filter to WHERE clause of generated UPDATE and DELETE statement using EF Core when you call SaveChanges? (Quite similar to Global query filters which work just for SELECT scenarios.)

I'm going to use such feature for a multi-tenancy scenario in a small application and I'm able to handle this requirement in other layers, like API, DAL, BLL or by overriding SaveChanges and checking if the deleted or modified entity belongs to the right tenant; The main reason that I prefer to not do it in BLL or SaveChanges, is because of performance. A WHERE clause will be evaluated at database server side, it's secure and also high performance, quite similar to global query filters.

For example, for a Delete scenario, an obvious delete scenario could be something like this:

var entity = db.BlogPosts.Find(id);
if(entity!=null)
    db.Entry(entity).State = EntityState.Deleted;
db.SaveChanges();

Which is basically a SELECT and then a DELETE.

However, I prefer to delete like this, which is just a DELETE statement:

db.Entry(new BlogPost() { Id = id }).State = EntityState.Deleted;
db.SaveChanges();

I've already asked a question in stackoverflow: EF Core Global Query Filter for SaveChanges (Update, Delete) and answered my own question by using Command Interceptors; however I'd like to know:

  • Is there any equivalent for query filters for UPDATE and DELETE scenarios?
  • Has the feature been discussed and rejected/approved as part of the roadmap?

EF Core version: EF Core 5.0
Database provider: (any - but interested in MS SQL Server and SQLite)
Target framework: .NET 5.0

Dominant language
C#
Stars
14.8k
Forks
3.4k
Avg merge
2d 5h
Merged PRs (30d)
134

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from dotnet/efcore

All issues in dotnet/efcore

Similar issues

More C# issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.