EF6 Disable Query Plan Caching with Command Tree Interceptor

后端 未结 4 1965
执笔经年
执笔经年 2021-02-18 14:28

I\'m using IDbCommandTreeInterceptor to implement soft-delete functionality. Inside standard TreeCreated method I check whether given query command con

4条回答
  •  醉酒成梦
    2021-02-18 14:53

    To update softdelete, you can override SaveChanges method, and to create filter, you can use dbContext.Query() which will apply soft delete filter automatically using expression generator.

    To filter your soft delete column, you can implement following method, in your DbContext.

    public IQueryable Query(){
    
       var ds = this.Set() as IQueryable;
    
       var entityType = typeof(T);
    
       if(!softDeleteSupported)
            return ds;
    
       ParameterExpression pe = Expression.Parameter(entityType);
       Expression compare = Expression.Equals( 
              Expression.Property(pe, "SoftDeleted"),
              Expression.Constant(false));
    
       Expression> filter = 
           Expression.Lambda>(compare,pe);
    
       return ds.Where(filter);
    }
    

提交回复
热议问题