Пример #1
0
 public virtual IOrderedQueryable <TModel> GetSearchQuery()
 {
     if (typeof(TModel).IsSubclassOf(typeof(BasePoco)))
     {
         return(DC.GetSet <TModel>().OrderBy(x => (x as BasePoco).ID));
     }
     else
     {
         return(null);
     }
 }
Пример #2
0
        public virtual List <TModel> GetAll()
        {
            List <TModel> rv    = new List <TModel>();
            var           query = DC.GetSet <TModel>().AsQueryable();

            if (typeof(TModel).GetInterface("IMLData") != null)
            {
                query = DC.GetSet <TModel>().SInclude("MLContents");
            }
            if (_toInclude != null)
            {
                foreach (var item in _toInclude)
                {
                    query = query.Include(item);
                }
            }
            rv = query.ToList();
            return(rv);
        }
Пример #3
0
        public virtual TModel GetByID(long Id)
        {
            TModel rv    = null;
            var    query = DC.GetSet <TModel>().AsQueryable();

            if (typeof(TModel).GetInterface("IMLData") != null)
            {
                query = query.SInclude("MLContents");
            }
            if (_toInclude != null)
            {
                foreach (var item in _toInclude)
                {
                    query = query.Include(item);
                }
            }
            rv = query.Where(x => x.ID == Id).SingleOrDefault();
            return(rv);
        }
Пример #4
0
 public virtual void DoAdd()
 {
     DC.GetSet <TModel>().Add(Entity);
     DC.Commit();
 }
Пример #5
0
        protected void ValidateDuplicateData(List <ValidationResult> rv)
        {
            if (this._duplicatedPropertiesCheck != null && this._duplicatedPropertiesCheck.Groups.Count > 0)
            {
                var baseExp              = DC.GetSet <TModel>().AsQueryable();
                var modelType            = typeof(TModel);
                ParameterExpression para = Expression.Parameter(modelType, "tm");
                foreach (var group in this._duplicatedPropertiesCheck.Groups)
                {
                    List <Expression> conditions = new List <Expression>();
                    //生成一个表达式,类似于 x=>x.ID != id
                    MemberExpression   idLeft     = Expression.Property(para, "ID");
                    ConstantExpression idRight    = Expression.Constant(Entity.ID);
                    BinaryExpression   idNotEqual = Expression.NotEqual(idLeft, idRight);
                    conditions.Add(idNotEqual);
                    List <PropertyInfo> props = new List <PropertyInfo>();
                    foreach (var field in group.Fields)
                    {
                        if (field.DirectFieldExp != null)
                        {
                            var    item         = field.DirectFieldExp;
                            string propertyName = UtilsTool.GetPropertyName(item);
                            var    prop         = UtilsTool.GetPropertyInfo(item);
                            props.Add(prop);
                            var func = item.Compile();
                            var val  = func.Invoke(this.Entity);

                            if (val == null)
                            {
                                continue;
                            }
                            if (val is string && val.ToString() == "")
                            {
                                var requiredAttrs = prop.GetCustomAttributes(typeof(RequiredAttribute), false);

                                if (requiredAttrs == null || requiredAttrs.Length == 0)
                                {
                                    continue;
                                }
                                else
                                {
                                    var requiredAtt = requiredAttrs[0] as RequiredAttribute;
                                    if (requiredAtt.AllowEmptyStrings == true)
                                    {
                                        continue;
                                    }
                                }
                            }
                            //生成一个表达式,类似于 x=>x.field == val
                            Expression left = Expression.Property(para, propertyName);
                            if (left.Type.IsGenericType && left.Type.GetGenericTypeDefinition() == typeof(Nullable <>))
                            {
                                left = Expression.Property(left, "Value");
                            }
                            if (left.Type == typeof(string))
                            {
                                left = Expression.Call(left, typeof(String).GetMethod("Trim", Type.EmptyTypes));
                            }
                            if (val is string)
                            {
                                val = val.ToString().Trim();
                            }
                            ConstantExpression right = Expression.Constant(val);
                            BinaryExpression   equal = Expression.Equal(left, right);
                            conditions.Add(equal);
                        }
                        else
                        {
                            dynamic    dField = field;
                            Expression exp    = dField.GetExpression(Entity, para);
                            if (exp != null)
                            {
                                conditions.Add(exp);
                            }
                            props.AddRange(dField.GetProperties());
                        }
                    }

                    int count = 0;
                    if (conditions.Count > 1)
                    {
                        Expression conExp = conditions[0];
                        for (int i = 1; i < conditions.Count; i++)
                        {
                            conExp = Expression.And(conExp, conditions[i]);
                        }

                        MethodCallExpression whereCallExpression = Expression.Call(
                            typeof(Queryable),
                            "Where",
                            new Type[] { modelType },
                            baseExp.Expression,
                            Expression.Lambda <Func <TModel, bool> >(conExp, new ParameterExpression[] { para }));
                        var result = baseExp.Provider.CreateQuery(whereCallExpression);
                        foreach (var res in result)
                        {
                            count++;
                        }
                    }
                    if (count > 0)
                    {
                        string AllName = "";
                        foreach (var prop in props)
                        {
                            var    dispAttrs = prop.GetCustomAttributes(typeof(DisplayAttribute), false);
                            string name      = prop.Name;
                            if (name == "LanguageCode")
                            {
                                continue;
                            }
                            if (dispAttrs != null && dispAttrs.Length > 0)
                            {
                                var attr = dispAttrs[0] as DisplayAttribute;
                                name = attr.Name;
                                if (attr.ResourceType != null)
                                {
                                    name = attr.ResourceType.GetProperty(name).GetValue(null, null).ToString();
                                }
                            }
                            AllName += name + ",";
                        }
                        if (AllName.EndsWith(","))
                        {
                            AllName = AllName.Remove(AllName.Length - 1);
                        }
                        if (props.Count == 1)
                        {
                            rv.Add(new ValidationResult(AllName + Resources.Language.字段重复, GetValidationFieldName(props[0])));
                        }
                        else if (props.Count > 1)
                        {
                            rv.Add(new ValidationResult(AllName + Resources.Language.组合字段重复, GetValidationFieldName(props[0])));
                        }
                        else
                        {
                            rv.Add(new ValidationResult(AllName + Resources.Language.组合字段重复));
                        }
                    }
                }
            }
        }
Пример #6
0
 public virtual void DoDelete()
 {
     DC.DeleteEntity(DC.GetSet <TModel>(), Entity);
     DC.Commit();
 }