EF Core Global Query Filter for SaveChanges (Update, Delete)
Nobody has claimed this yet.
Assessment
- Difficulty
- 5/5
- Estimated time
- Over a week
- Newbie friendliness
- 25/100
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
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
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from dotnet/efcore
-
Difficulty 4/5 3-5 days Newbie friendliness 55/100
-
customer-reported
Difficulty 5/5 Over a week Newbie friendliness 38/100
-
area-cosmos area-vector-search
Difficulty 5/5 Over a week Newbie friendliness 25/100
-
area-cosmos
Difficulty 5/5 Over a week Newbie friendliness 25/100
-
area-tools needs-design
Difficulty 4/5 3-5 days Newbie friendliness 25/100
Similar issues
-
Difficulty 2/5 1-3 hours Newbie friendliness 86/100
-
:watch: Not Triaged 11.0 fundamentals/subsvc
Difficulty 2/5 1-3 hours Newbie friendliness 92/100
dotnet/AspNetCore.Docs#37699 ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
SubtitleEdit/subtitleedit#15108 · 1 comment ·
-
area/docs-content Bug pulumi/docs
Difficulty 1/5 1-3 hours Newbie friendliness 94/100
-
agentic-workflows untriaged
Difficulty 2/5 1-3 hours Newbie friendliness 76/100