如何检查特定表的每个存储库命中的特定列?

问题描述 投票:0回答:1

技术:.Net Core,EF Core 3.0,Abp

在表上获得一个“启用”列,而不是通过每次点击Repository.GetAll()并添加.Where(w => w.Enabled)的方法,我可以将其设为默认检查?

我在“全局过滤器”中进行了搜索,但未完全找到我认为的期望。

欢呼声

.net entity-framework aspnetboilerplate ef-core-3.0 abp
1个回答
0
投票

据我了解,这正是全局过滤器的用途。在您的DbContext中,您将指定哪些表具有IsEnabled标志,并且该标志将被自动应用。

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);
    }
}

如果碰巧有不需要应用全局过滤器的查询,则可以在查询中使用IgnoreQueryFilters()。

using (var context = new MyContext())
{
    var customers = context.Customers
        .IgnoreQueryFilters().ToList();
}
© www.soinside.com 2019 - 2024. All rights reserved.