EFCore 定义全局过滤器
在数据库上下文中的OnModelCreating()方法中增加全局过滤器,来统一过滤:
protected override void OnModelCreating(ModelBuilder modelBuilder) { #region 其他代码 modelBuilder.Entity() .Property(catetogy => catetogy.CategoryName) .IsRequired() .HasMaxLength(15); modelBuilder.Entity () .Property(product => product.Cost) .HasConversion<double>(); #endregion //添加全局过滤器用来过滤掉停产的产品,这样在每次写查询时都不用单独写where条件来过滤了 modelBuilder.Entity () .HasQueryFilter(p => !p.Discontinued); //全局过滤器 }