public static DbExpression Convert(DynamicFilterDefinition filter, DbExpressionBinding binding, ObjectContext objectContext)
        {
            var visitor    = new LambdaToDbExpressionVisitor(filter, binding, objectContext);
            var expression = visitor.Visit(filter.Predicate) as LambdaExpression;

            return(visitor.GetDbExpressionForExpression(expression.Body));
        }
示例#2
0
        public DynamicFilterConvention(string filterName, Type entityType, LambdaExpression predicate,
                                       string columnName)
        {
            var configuration = Types().Where(entityType.IsAssignableFrom);

            configuration.Configure(ctc =>
            {
                var filterDefinition = new DynamicFilterDefinition(filterName, predicate, columnName, ctc.ClrType);

                ctc.HasTableAnnotation(filterDefinition.AttributeName, filterDefinition);
            });
        }
示例#3
0
        public static EntityTypeConfiguration <TEntity> Filter <TEntity>(this EntityTypeConfiguration <TEntity> config,
                                                                         string filterName, string columnName, object globalValue = null)
            where TEntity : class
        {
            filterName = ScrubFilterName(filterName);
            var filterDefinition = new DynamicFilterDefinition(filterName, null, columnName, typeof(TEntity));

            config.HasTableAnnotation(filterDefinition.AttributeName, filterDefinition);

            //  Always add the filter to _GlobalParameterValues - need it to be able to disable it
            _GlobalParameterValues.TryAdd(filterName, new DynamicFilterParameters());

            if (globalValue != null)
            {
                SetFilterGlobalParameterValue(null, filterName, columnName, globalValue);
            }

            return(config);
        }
示例#4
0
        internal static void SetSqlParameters(this DbContext context, DbCommand command)
        {
            foreach (DbParameter param in command.Parameters)
            {
                //  Item1 = Filter Name
                //  Item2 = Parameter/Column Name
                var filterNameAndParam = DynamicFilterDefinition.GetFilterAndParamFromDBParameter(param.ParameterName);
                if (filterNameAndParam == null)
                {
                    continue;       //  Not dynamic filter param
                }
                object value = context.GetFilterParameterValue(filterNameAndParam.Item1, filterNameAndParam.Item2);

                //  If not found, set to DBNull.  It should already be set to that, but it's not in Postgre and we get
                //  errors if we don't do that now.
                if (value == null)
                {
                    param.Value = DBNull.Value;
                }
                else
                {
                    //  Check to see if it's a collection.  If so, this is an "In" parameter
                    var valueType = value.GetType();
                    if (valueType.IsGenericType && typeof(IEnumerable).IsAssignableFrom(valueType))
                    {
                        //  Generic collection.  The EF query created for this collection was an '=' condition.
                        //  We need to convert this into an 'in' clause so that we can dynamically set the
                        //  values in the collection.
                        SetParameterList(value as IEnumerable, param, command, IsOracle(context));
                        context.Database.Log(string.Format("Manually replaced single parameter value with list, new SQL=\r\n{0}", command.CommandText));
                    }
                    else
                    {
                        param.Value = value;
                    }
                }
            }
        }
 private LambdaToDbExpressionVisitor(DynamicFilterDefinition filter, DbExpressionBinding binding, ObjectContext objectContext)
 {
     _Filter        = filter;
     _Binding       = binding;
     _ObjectContext = objectContext;
 }