【问题标题】:How to check a specific column on every Repository hit for a specific table?如何检查特定表的每个存储库命中的特定列?
【发布时间】:2020-04-29 14:57:19
【问题描述】:

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

在表格上有一个“已启用”列,而不是遍历每个 Repository.GetAll() 并添加 .Where(w => w.Enabled),有没有办法可以将此作为默认检查?

我搜索了“全局过滤器”,但没有完全找到我认为我所期待的。

干杯

【问题讨论】:

    标签: .net entity-framework aspnetboilerplate ef-core-3.0 abp


    【解决方案1】:

    据我了解,这正是全局过滤器的用途。 在您的 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();
    }
    

    【讨论】:

    • 这正是我一直在寻找的,但我似乎找不到具体的答案。干杯!
    • 不客气!如果这回答了您的问题,请接受作为答案。它可以帮助将来通过 Google 或其他方式找到此帖子的人 :-)
    猜你喜欢
    • 1970-01-01
    • 2011-10-17
    • 2021-12-14
    • 1970-01-01
    • 2019-01-17
    • 2016-02-23
    • 1970-01-01
    • 1970-01-01
    • 2021-08-10
    相关资源
    最近更新 更多