Tech: .Net Core, EF Core 3.0, Abp
Got an "Enabled" column on a table and instead of going through every Repository.GetAll()
hit and add .Where(w => w.Enabled)
is there a way I can make this a default check?
I have had a search around "Global Filters" but not exactly finding what I think I'm expecting.
Cheers
As per my understanding, that is exactly what Global Filters are for. In your DbContext, you would specify what tables have an IsEnabled flag, and it would be automatically applied.
https://entityframeworkcore.com/querying-data-global-filter
public class MyContext : DbContext
{
public DbSet<Customer> Customers { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseSqlServer(@"Data Source=(localdb)\ProjectsV13;Initial Catalog=CustomerDB;");
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Customer>().HasQueryFilter(c => !c.IsDeleted);
}
}
If you happen to have any queries that don't need the global filter applied, you can use IgnoreQueryFilters() on your query.
using (var context = new MyContext())
{
var customers = context.Customers
.IgnoreQueryFilters().ToList();
}