Entity Framework global query filters, .NET 10 will support multiple
Posted: (EET/GMT+2)
With Entity Framework, it is possible to define global query filters. Shortly put, these are regular LINQ expressions on an entity type that all applied to all queries run against the database. In a way, it is a way to say: "When I query my database for these entities, always add this to the WHERE clause." To set up these, you would add the a line like this to your EF context object’s OnModelCreating method:
modelBuilder.Entity().HasQueryFilter(b => !b.IsDeleted);
In the soon-here .NET version 10 (and thus, EF version 10), there's additional support for multiple query filters at one:
modelBuilder.Entity() .HasQueryFilter("SoftDeletionFilter", c => !c.IsDeleted) .HasQueryFilter("TenantFilter", c => c.TenantId == tenantId);
Notice how a unique name must be given for each multiple query filter if they use the same entity object.