1024programmer Asp.Net EFCore uses FluntApi to configure global query filters

EFCore uses FluntApi to configure global query filters

EFCore uses FluntApi to configure global query filters

We usually have an attribute called IsDel in the class to represent soft deletion or logical deletion. This attribute will cause us to have to .where(s=>s.IsDel== every time when performing query operations) false) is very troublesome. This problem can be solved well by configuring query filters when using efCore.

public class SysUser
 {
     public long Id { get; set; }
     public bool IsDel { get; set; } = false;
     public DateTime CreateTime { get; set; }
 }
 

The IsDel property in our SysUser class represents our soft delete.

My own DbContext class here is called MyDbContext

public class MyDbContext:DbContext
 

Override the OnModelCreating method in MyDbContext to configure the query filter for the SysUser class

protected override void OnModelCreating(ModelBuilder modelBuilder)
 {
     base.OnModelCreating(modelBuilder);
     modelBuilder.Entity().HasQueryFilter(u => u.IsDel == false);
 }
 

But since there will be many entity classes in our project, it is a bit bloated to write them all here.
We can create a new EntityBase folder or project (the specific method depends on personal habits), I created a new folder here.
Create a new class in the folder and implement the IEntityTypeConfiguration interface to configure the entity class

 public class SysUserEntityConfig : IEntityTypeConfiguration
  {
      public void Configure(EntityTypeBuilder builder)
      {
          builder.HasQueryFilter(u => u.IsDel == false);
      }
  }
 

Then in the OnModelCreating method, all entity configuration classes that implement the IEntityTypeConfiguration interface are automatically applied from the assembly where the current class is located through the modelBuilder.ApplyConfigurationsFromAssembly(this.GetType().Assembly) method.

protected override void OnModelCreating(ModelBuilder modelBuilder)
 {
     base.OnModelCreating(modelBuilder);
     modelBuilder.ApplyConfigurationsFromAssembly(this.GetType().Assembly);
 }
 

If you are not in the same assembly, you can use this method

protected override void OnModelCreating(ModelBuilder modelBuilder)
 {
     base.OnModelCreating(modelBuilder);
     var assembly = Assembly.Load("EntityConfigurations(replace this with your actual assembly)");
     modelBuilder.ApplyConfigurationsFromAssembly(assembly);
 }
 

In this way we can implement global query filtering when using efCore.

This article is from the internet and does not represent1024programmerPosition, please indicate the source when reprinting:https://www.1024programmer.com/efcore-uses-fluntapi-to-configure-global-query-filters/

author: admin

Previous article
Next article

Leave a Reply

Your email address will not be published. Required fields are marked *

Contact Us

Contact us

181-3619-1160

Online consultation: QQ交谈

E-mail: [email protected]

Working hours: Monday to Friday, 9:00-17:30, holidays off

Follow wechat
Scan wechat and follow us

Scan wechat and follow us

Follow Weibo
Back to top
首页
微信
电话
搜索