Ejemplo n.º 1
0
        /// <summary>
        /// 返回满足条件的一组实体对象。
        /// </summary>
        /// <param name="condition">一般的条件语句。</param>
        /// <param name="orderBy">排序语句。</param>
        /// <param name="segment">数据分段对象。</param>
        /// <param name="parameters">查询参数集合。</param>
        /// <returns></returns>
        public virtual IEnumerable <object> Query(string condition, string orderBy, IDataSegment segment = null, ParameterCollection parameters = null)
        {
            var syntax = Database.Provider.GetService <ISyntaxProvider>();
            var query  = new EntityQueryBuilder(syntax, Environment, GetEntityType(), parameters).Select().All().From().Where(condition).OrderBy(orderBy);

            return(Database.InternalExecuteEnumerable(GetEntityType(), query.ToSqlCommand(), segment, parameters).Cast <object>());
        }
Ejemplo n.º 2
0
        public IEnumerable <TEntity> Where(string condition, string orderBy, IDataSegment segment = null, ParameterCollection parameters = null)
        {
            var syntax = context.Database.Provider.GetService <ISyntaxProvider>();
            var query  = new EntityQueryBuilder(syntax, null, typeof(TEntity), parameters).Select().All().From().Where(condition).OrderBy(orderBy);

            return(context.Database.InternalExecuteEnumerable <TEntity>(query.ToSqlCommand(), segment, parameters));
        }
Ejemplo n.º 3
0
        /// <summary>
        /// 检查指定的值对于当前的验证特性是否有效。
        /// </summary>
        /// <param name="value">属性值。</param>
        /// <param name="validationContext">验证上下文对象。</param>
        /// <returns></returns>
        protected override ValidationResult IsValid(object value, ValidationContext validationContext)
        {
            Guard.ArgumentNull(validationContext, "validationContext");

            var entityType = validationContext.ObjectType;

            if (ValidateNullable(value) == ValidationResult.Success)
            {
                return(ValidationResult.Success);
            }

            IDatabase database;

            try
            {
                database = DatabaseFactory.GetDatabaseFromScope();
            }
            catch (UnableGetDatabaseScopeException exp)
            {
                return(new ValidationResult(exp.Message));
            }

            var paramaters = new ParameterCollection();
            var entity     = validationContext.ObjectInstance as IEntity;
            var syntax     = database.Provider.GetService <ISyntaxProvider>();

            var eqb = new EntityQueryBuilder(syntax, null, entityType, paramaters);

            eqb = eqb.Select().Count().From().Where(Property, value);

            if (entity.EntityState != EntityState.Attached)
            {
                foreach (var pk in PropertyUnity.GetPrimaryProperties(entityType))
                {
                    eqb = eqb.And(pk, entity.GetValue(pk.Name), QueryOperator.UnEquals);
                }
            }

            var result = database.ExecuteScalar <int>(eqb.ToSqlCommand(), paramaters);

            return(result == 0 ? ValidationResult.Success : new ValidationResult(FormatErrorMessage(validationContext.DisplayName)));
        }