protected DBResult Delete <T>(T entity)
            where T : class
        {
            var  entityID    = EntityHelper.GetEntityID(entity);
            bool isValueType = entityID.GetType().IsValueType;

            if (entity == null || (isValueType && (int)entityID == 0) || (!isValueType && string.IsNullOrEmpty((string)entityID)))
            {
                return(DBResult.WrongParameter);
            }

            var contextName = DBDictionary.DBEntitiesDictionary.FirstOrDefault(i => i.Value.Contains(typeof(T))).Key;

            if (string.IsNullOrEmpty(contextName))
            {
                return(DBResult.Unknown);
            }

            using (var db = (DbContext)GetDbContext(contextName))
            {
                var dbSet = db.Set <T>();

                var lambda = DynamicLinqConstructor.PropertyEqual <T>("ID", entityID);
                var target = dbSet.FirstOrDefault(lambda);
                if (target == null)
                {
                    return(DBResult.NotFound);
                }

                dbSet.Remove(target);
                db.SaveChanges();
                return(DBResult.Succeed);
            }
        }
        protected DBResult CheckExists <T>(DbSet <T> dbSet, string[] propertyNames, T entity, bool excludeSelf = false)
            where T : class
        {
            if (dbSet == null)
            {
                return(DBResult.WrongParameter);
            }

            if (propertyNames == null)
            {
                return(DBResult.Succeed);
            }

            if (entity == null)
            {
                return(DBResult.WrongParameter);
            }

            var query      = dbSet.AsQueryable();
            var properties = entity.GetType().GetProperties();

            if (excludeSelf)
            {
                var entityID = EntityHelper.GetEntityID(entity);
                if (entityID != null)
                {
                    var Lambda = DynamicLinqConstructor.PropertyNotEqual <T>("ID", entityID);
                    query = dbSet.Where(Lambda);
                }
            }

            foreach (var propertyName in propertyNames)
            {
                var property = properties.FirstOrDefault(p => p.Name == propertyName);
                if (property == null)
                {
                    continue;
                }

                var objectValue   = property.GetValue(entity);
                var propertyType  = property.PropertyType;
                var propertyValue = Convert.ChangeType(objectValue, propertyType);

                var Lambda = DynamicLinqConstructor.PropertyEqual <T>(propertyName, propertyValue);
                if (query.Any(Lambda))
                {
                    var result = DBDictionary.ExistsDictionary.FirstOrDefault(i => i.Key == propertyName);
                    if (string.IsNullOrEmpty(result.Key))
                    {
                        return(DBResult.PropertyValueExisted);
                    }

                    return(result.Value);
                }
            }

            return(DBResult.Succeed);
        }
        protected IQueryable <T> SetQueryOnly <T>(IQueryable <T> query, QueryConditions queryConditions, out int totalCount)
            where T : class
        {
            totalCount = 0;

            var queryProperties = queryConditions.GetType().GetProperties()
                                  .Where(p => p.Name != "PageIndex" && p.Name != "PageSize" && p.Name != "OrderBy" && p.Name != "OrderByDescending").ToArray();

            foreach (var queryProperty in queryProperties)
            {
                if (queryConditions.IgnoredProperties != null && queryConditions.IgnoredProperties.Contains(queryProperty.Name))
                {
                    continue;
                }

                var queryValue = queryProperty.GetValue(queryConditions);
                var queryType  = queryProperty.PropertyType;

                if (queryType == typeof(bool))
                {
                    var lambda = DynamicLinqConstructor.PropertyEqual <T>(queryProperty.Name, queryValue);
                    query = query.Where(lambda);
                }
                else if (queryType.IsGenericType && queryType.GetGenericTypeDefinition() == typeof(Nullable <>))
                {
                    if (queryValue == null)
                    {
                        continue;
                    }

                    var typedValue = Convert.ChangeType(queryValue, queryType.GetGenericArguments()[0]);

                    var lambda = DynamicLinqConstructor.PropertyBetween <T>(queryProperty.Name, typedValue);
                    query = query.Where(lambda);
                }
                else if (queryType.IsValueType && !queryValue.Equals(Activator.CreateInstance(queryType)))
                {
                    var lambda = DynamicLinqConstructor.PropertyBetween <T>(queryProperty.Name, queryValue);
                    query = query.Where(lambda);
                }
                else if (queryType == typeof(string) && !string.IsNullOrEmpty((string)queryValue))
                {
                    var lambda = DynamicLinqConstructor.PropertyContains <T>(queryProperty.Name, (string)queryValue);
                    query = query.Where(lambda);
                }
            }

            totalCount = query.Count();
            return(query);
        }
        protected DBResult EditAll <T>(List <T> entities, string[] ignoreNullCheck = null, string[] checkExists = null)
            where T : class
        {
            if (entities == null)
            {
                return(DBResult.WrongParameter);
            }

            var contextName = DBDictionary.DBEntitiesDictionary.FirstOrDefault(i => i.Value.Contains(typeof(T))).Key;

            if (string.IsNullOrEmpty(contextName))
            {
                return(DBResult.Unknown);
            }

            using (var db = (DbContext)GetDbContext(contextName))
            {
                var dbSet = db.Set <T>();
                foreach (var entity in entities)
                {
                    if (EntityHelper.HasNullProperty(entity, ignoreNullCheck))
                    {
                        return(DBResult.WrongParameter);
                    }

                    var lambda = DynamicLinqConstructor.PropertyEqual <T>("ID", EntityHelper.GetEntityID(entity));
                    var target = dbSet.FirstOrDefault(lambda);
                    if (target == null)
                    {
                        return(DBResult.NotFound);
                    }

                    var existResult = CheckExists(dbSet, checkExists, entity, true);
                    if (existResult != DBResult.Succeed)
                    {
                        return(existResult);
                    }

                    EntityHelper.CopyEntity(entity, target);
                }

                db.SaveChanges();
                return(DBResult.Succeed);
            }
        }
        protected IQueryable <T> SetOrder <T>(IQueryable <T> query, string orderPropertyName, Type orderPropertyType, bool isDescending)
            where T : class
        {
            if (!isDescending)
            {
                if (orderPropertyType == typeof(int))
                {
                    var lambda = DynamicLinqConstructor.PropertySelect <T, int>(orderPropertyName);
                    query = query.OrderBy(lambda);
                }
                else if (orderPropertyType == typeof(decimal))
                {
                    var lambda = DynamicLinqConstructor.PropertySelect <T, decimal>(orderPropertyName);
                    query = query.OrderBy(lambda);
                }
                else if (orderPropertyType == typeof(DateTime))
                {
                    var lambda = DynamicLinqConstructor.PropertySelect <T, DateTime>(orderPropertyName);
                    query = query.OrderBy(lambda);
                }
                else if (orderPropertyType == typeof(string))
                {
                    var lambda = DynamicLinqConstructor.PropertySelect <T, string>(orderPropertyName);
                    query = query.OrderBy(lambda);
                }
                else if (orderPropertyType == typeof(Nullable <int>))
                {
                    var lambda = DynamicLinqConstructor.PropertySelect <T, Nullable <int> >(orderPropertyName);
                    query = query.OrderBy(lambda);
                }
                else if (orderPropertyType == typeof(Nullable <decimal>))
                {
                    var lambda = DynamicLinqConstructor.PropertySelect <T, Nullable <decimal> >(orderPropertyName);
                    query = query.OrderBy(lambda);
                }
            }
            else
            {
                if (orderPropertyType == typeof(int))
                {
                    var lambda = DynamicLinqConstructor.PropertySelect <T, int>(orderPropertyName);
                    query = query.OrderByDescending(lambda);
                }
                else if (orderPropertyType == typeof(decimal))
                {
                    var lambda = DynamicLinqConstructor.PropertySelect <T, decimal>(orderPropertyName);
                    query = query.OrderByDescending(lambda);
                }
                else if (orderPropertyType == typeof(DateTime))
                {
                    var lambda = DynamicLinqConstructor.PropertySelect <T, DateTime>(orderPropertyName);
                    query = query.OrderByDescending(lambda);
                }
                else if (orderPropertyType == typeof(string))
                {
                    var lambda = DynamicLinqConstructor.PropertySelect <T, string>(orderPropertyName);
                    query = query.OrderByDescending(lambda);
                }
                else if (orderPropertyType == typeof(Nullable <int>))
                {
                    var lambda = DynamicLinqConstructor.PropertySelect <T, Nullable <int> >(orderPropertyName);
                    query = query.OrderByDescending(lambda);
                }
                else if (orderPropertyType == typeof(Nullable <decimal>))
                {
                    var lambda = DynamicLinqConstructor.PropertySelect <T, Nullable <decimal> >(orderPropertyName);
                    query = query.OrderByDescending(lambda);
                }
            }

            return(query);
        }