ef core 2 apply HasQueryFilter for all entity -
is there way apply "hasqueryfilter" globaly entity ? don't want add in modelbuilder 1 one ?
modelbuilder.entity<manufacturer>().hasqueryfilter(p => p.isactive);
in case have base class or interface defining isactive
property, use approach ef-core 2.0 filter queries (trying achieve soft delete).
otherwise iterate entity types, , each type having bool isactive
property build dynamically filter expression using expression
class methods:
foreach (var entitytype in modelbuilder.model.getentitytypes()) { var isactiveproperty = entitytype.findproperty("isactive"); if (isactiveproperty != null && isactiveproperty.clrtype == typeof(bool)) { var parameter = expression.parameter(entitytype.clrtype, "p"); var filter = expression.lambda(expression.property(parameter, isactiveproperty.propertyinfo), parameter); entitytype.queryfilter = filter; } }
wiki
Comments
Post a Comment