示例#1
0
        private ValidationResult DirectlyValid(object value, ValidationContext validationContext)
        {
            using (CFContext db = new CFContext())
            {
                string Name = GetName(validationContext);

                PropertyInfo IdProp = validationContext.ObjectInstance.GetType().GetProperties().FirstOrDefault(x => x.CustomAttributes.Count(a => a.AttributeType == typeof(KeyAttribute)) > 0);

                var Id = IdProp.GetValue(validationContext.ObjectInstance, null);

                Type entityType = validationContext.ObjectType;


                IQueryable result = db.Set(entityType).Where(Name + "==@0", value);
                int        count  = 0;

                if (Id != null || Name != IdProp.Name)
                {
                    result = result.Where(IdProp.Name + "<>@0", Id);
                }

                count = result.Count();

                if (count == 0 || value == null)
                {
                    return(ValidationResult.Success);
                }
                else
                {
                    return(new ValidationResult(ErrorMessageString));
                }
            }
        }
示例#2
0
        public static IEnumerable <T> GetAll(string[] includes = null)
        {
            //HANDLE INCLUDES FOR ASSOCIATED OBJECTS IF APPLICABLE
            if (includes != null && includes.Count() > 0)
            {
                var query = dbContext.Set <T>().Include(includes.First());
                foreach (var include in includes.Skip(1))
                {
                    query = query.Include(include);
                }
                return(query.AsQueryable());
            }

            return(dbContext.Set <T>().AsQueryable());
        }