码迷,mamicode.com
首页 > Web开发 > 详细

asp.net core 2.0 全局过滤(HasQueryFilter)

时间:2018-11-24 11:33:45      阅读:315      评论:0      收藏:0      [点我收藏+]

标签:builder   prope   rom   info   pes   elb   png   文章   ons   

  本文章主要是为了总结自己遇到的问题,同时也是别人的已经最哦好的成果。

  HasQueryFilter主要用于数据软删除,还习惯用delete的小伙伴,你们out了。。。

  首先设置一个基类,BaseEntity,包含IsDeleted属性:

  技术分享图片

  所有用到软删除的类继承于此类,然后在MyDbContext中的OnModelCreating方法中实现全局过滤:

  技术分享图片

  代码如下:

foreach (var entityType in modelBuilder.Model.GetEntityTypes()
                .Where(e=>typeof(BaseEntity).IsAssignableFrom(e.ClrType)))
            {
                modelBuilder.Entity(entityType.ClrType).Property<bool>("IsDeleted");
                var parameter = Expression.Parameter(entityType.ClrType, "e");
                var body = Expression.Equal(
                    Expression.Call(typeof(EF), nameof(EF.Property), new[] { typeof(bool) }, parameter, Expression.Constant("IsDeleted")),
                    Expression.Constant(false));
                modelBuilder.Entity(entityType.ClrType).HasQueryFilter(Expression.Lambda(body, parameter));
            }

  

   然后就是普通查询就ok了。。。

  过程解析可以查看大佬的博客:

  https://www.cnblogs.com/CreateMyself/p/8491058.html

 

asp.net core 2.0 全局过滤(HasQueryFilter)

标签:builder   prope   rom   info   pes   elb   png   文章   ons   

原文地址:https://www.cnblogs.com/hany-sundigital/p/10011050.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!