Ejemplo n.º 1
0
        public int UpdateBatch <TEntity>(Expression <Func <TEntity, TEntity> > evaluator, Expression <Func <TEntity, bool> > filter = null)
            where TEntity : class
        {
            string tableName = AttributesHelper.GetTableName <TEntity>();
            List <SqlParameter> parameters = new List <SqlParameter>();
            int    memberInitCount         = 1;
            string setString = null;

            evaluator.Visit <MemberInitExpression>(delegate(MemberInitExpression expression)
            {
                if (memberInitCount > 1)
                {
                    throw new NotImplementedException("Currently only one MemberInitExpression is allowed for the evaluator parameter.");
                }
                memberInitCount++;
                setString = GetDbSetStatement <TEntity>(expression, parameters);
                return(expression);
            });
            string whereString = null;

            if (filter != null)
            {
                whereString = GetBatchConditionQuery(filter, parameters);
            }
            string sql = string.Format("UPDATE {0}\r\n{1}\r\n\r\n{2}", tableName, setString, whereString);

            return(this.Database.ExecuteSqlCommand(sql, parameters.ToArray()));
        }
Ejemplo n.º 2
0
        public static void SetCache <TEntity>()
            where TEntity : class
        {
            var    db        = CoreDBContext.GetContext();
            string tableName = AttributesHelper.GetTableName <TEntity>();

            SetValue(tableName, db.Set <TEntity>().ToList());
        }
Ejemplo n.º 3
0
        public static List <TEntity> GetCacheOfEntity <TEntity>()
            where TEntity : class
        {
            string tableName = AttributesHelper.GetTableName <TEntity>();

            if (!Exists(tableName))
            {
                SetCache <TEntity>();
            }

            return(GetEntitys <TEntity>(tableName));
        }
Ejemplo n.º 4
0
        public static object GetPropertyValue(object[] entitys, string tableName, string fieldName)
        {
            object entity = null;

            foreach (var item in entitys)
            {
                if (item != null && AttributesHelper.GetTableName(item.GetType()) == tableName)
                {
                    entity = item;
                    break;
                }
            }
            if (entity == null)
            {
                throw new Exception(string.Format("未能从参数中解析到表名为{0}的数据!", tableName));
            }
            return(GetPropertyValue(entity, fieldName));
        }
Ejemplo n.º 5
0
        public int Delete <T>(Expression <Func <T, bool> > predicate) where T : class
        {
            string tableName = AttributesHelper.GetTableName <T>();

            if (!string.IsNullOrEmpty(tableName))
            {
                string           command          = String.Format("DELETE FROM {0}", tableName);
                ConditionBuilder conditionBuilder = new ConditionBuilder();
                conditionBuilder.Build(predicate.Body);
                if (!String.IsNullOrEmpty(conditionBuilder.Condition))
                {
                    command += " WHERE " + conditionBuilder.Condition;
                }

                return(this.Database.ExecuteSqlCommand(command, conditionBuilder.Arguments));
            }

            return(0);
        }
Ejemplo n.º 6
0
        public virtual void Delete(string[] ids)
        {
            string tableName = AttributesHelper.GetTableName <TEntity>();

            if (!string.IsNullOrEmpty(tableName))
            {
                string command = string.Format("DELETE FROM {0}", tableName);
                if (ids.Length > 0)
                {
                    string ary = string.Empty;
                    foreach (var item in ids)
                    {
                        ary += item + ",";
                    }
                    ary      = ary.Trim(new char[] { ',' });
                    command += string.Format(" WHERE ID IN({0})", ary);
                }

                this.DB.Database.ExecuteSqlCommand(command, new string[0]);
            }
        }
Ejemplo n.º 7
0
        protected static T GetCache <T>()
            where T : class
        {
            Type   type      = TypeHelper.GetChildType(typeof(T));
            string tableName = AttributesHelper.GetTableName(type);
            string cacheName = AttributesHelper.GetCacheName(type);

            if (!string.IsNullOrEmpty(tableName))
            {
                var cache = CacheProvider.GetCache <T>(tableName);
                if (cache == null)
                {
                    lock (lockObj)
                    {
                        cache = CacheProvider.GetCache <T>(tableName);
                        if (cache == null && GetData != null)
                        {
                            var list = GetData.GetInvocationList();
                            for (int i = list.Length - 1; i >= 0; i--)
                            {
                                try
                                {
                                    cache = list[i].DynamicInvoke(tableName) as T;
                                }
                                catch
                                {
                                }
                                if (cache != null)
                                {
                                    break;
                                }
                            }
                            CacheProvider.AddCache(tableName, cache, cacheName ?? tableName);
                        }
                    }
                }
                return(cache);
            }
            throw new WarningException("无法缓存未设置表名的实体");
        }