dotnet / efcore

EF Core is a modern object-database mapper for .NET. It supports LINQ queries, change tracking, updates, and schema migrations.
https://docs.microsoft.com/ef/
MIT License
13.8k stars 3.2k forks source link

How to batch configure global filters on soft deleted entities and non-soft deleted entities #35145

Closed ByZhouhang closed 52 minutes ago

ByZhouhang commented 6 days ago

The official document Global Query Filters link is as follows:

https://learn.microsoft.com/en-us/ef/core/querying/filters#accessing-entity-with-query-filter-using-required-navigation

When I configure global filtering on an entity, it is officially recommended The corresponding navigation entity must also be configured again, written as follows:

modelBuilder.Entity<Blog>().HasQueryFilter(b => b.Url.Contains("fish"));
modelBuilder.Entity<Post>().HasQueryFilter(p => p.Blog.Url.Contains("fish"));

Some entities in my project use soft deletion, but there is a one-to-one or one-to-many relationship between the soft-deleted entities and many other entities that are not soft-deleted. If you follow the configuration method of the document, it would be too much to configure them one by one. It’s troublesome and easy to miss. Is there a way to configure it in batches?

cincuranet commented 6 days ago

There's no in-the-box way to configure in batches. On the other hand, it shouldn't be that difficult to build some infrastructure yourself with little bit of reflection and some attribute.