-
Notifications
You must be signed in to change notification settings - Fork 226
Global Query Filters
Simon Hughes edited this page May 6, 2025
·
1 revision
To add global query filters as suggested here https://learn.microsoft.com/en-us/ef/core/querying/filter
In your <database>.tt file, set
Settings.DbContextClassModifiers = "public partial";Take a look at the generated DB context. You will see a call to OnModelCreatingPartial, which you can provide in a partial class and implement your query filters in.
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
...
OnModelCreatingPartial(modelBuilder); // <<----------- new
}For example, add a class that is a public partial of your own db context, and supply your own query filters inside the OnModelCreatingPartial function.
namespace V9EfrpgTest;
public partial class V9EfrpgTestDbContext
{
partial void OnModelCreatingPartial(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Car>().HasQueryFilter(p => p.CarMake != string.Empty);
}
}