public static DbContextEntitiesResult Error(string error) { DbContextEntitiesResult result = new DbContextEntitiesResult(); result.Errors.Add(error); return(result); }
public DbContextEntitiesResult GetAll(HttpContext httpContext, string contextName, DbContextEntityInfo entityInfo, Dictionary <string, string> where, int?page) { DbContextEntitiesResult results = new DbContextEntitiesResult(); results.Fields = entityInfo.Fields; try { Type dbContextType = GetDbContextTypes().FirstOrDefault(y => y.Name == contextName); DbContext dbContext = httpContext?.RequestServices?.GetService(dbContextType) as DbContext; if (dbContext == null) { return(DbContextEntitiesResult.Error("Cannot find DbContext: " + contextName)); } object dbSet = Reflector.InvokeGenericMethod(dbContext, "Set", new[] { entityInfo.ClrType }); ParameterExpression x = Expression.Parameter(entityInfo.ClrType, "x"); BinaryExpression lastOperation = null; foreach (var whereCondition in where) { var fieldInfo = entityInfo.Fields.FirstOrDefault(f => f.Name == whereCondition.Key); var operation = Expression.Equal( Expression.Call(typeof(EF), nameof(EF.Property), new[] { fieldInfo.ClrType }, x, Expression.Constant(whereCondition.Key)), Expression.Constant(Binder.ConvertToType(whereCondition.Value, fieldInfo.ClrType), fieldInfo.ClrType)); if (lastOperation == null) { lastOperation = operation; } else { lastOperation = Expression.And(lastOperation, operation); } } if (lastOperation != null) { Type delegateType = typeof(Func <,>).MakeGenericType(entityInfo.ClrType, typeof(bool)); LambdaExpression predicate = Expression.Lambda(delegateType, lastOperation, x); var whereMethod = typeof(System.Linq.Queryable).GetMethods(BindingFlags.Static | BindingFlags.Public) .FirstOrDefault(m => m.Name == "Where" && m.GetParameters().Count() == 2); MethodInfo genericWhereMethod = whereMethod.MakeGenericMethod(new[] { entityInfo.ClrType }); dbSet = genericWhereMethod.Invoke(null, new object[] { dbSet, predicate }); } int take = 200; int skip = (page.GetValueOrDefault(1) - 1) * 200; var skipMethod = typeof(System.Linq.Enumerable).GetMethods(BindingFlags.Static | BindingFlags.Public) .FirstOrDefault(m => m.Name == "Skip" && m.GetParameters().Count() == 2); MethodInfo genericSkipMethod = skipMethod.MakeGenericMethod(new[] { entityInfo.ClrType }); dbSet = genericSkipMethod.Invoke(null, new object[] { dbSet, skip }); //dbSet = typeof(System.Linq.Enumerable).InvokeStaticGenericMethod("Skip", new[] { entityInfo.ClrType }, dbSet, take); var takeMethod = typeof(System.Linq.Enumerable).GetMethods(BindingFlags.Static | BindingFlags.Public) .FirstOrDefault(m => m.Name == "Take" && m.GetParameters().Count() == 2); MethodInfo genericTakeMethod = takeMethod.MakeGenericMethod(new[] { entityInfo.ClrType }); dbSet = genericTakeMethod.Invoke(null, new object[] { dbSet, take }); //dbSet = typeof(System.Linq.Enumerable).InvokeStaticGenericMethod("Take", new[] { entityInfo.ClrType }, dbSet, take); var toListMethod = typeof(System.Linq.Enumerable).GetMethods(BindingFlags.Static | BindingFlags.Public) .FirstOrDefault(m => m.Name == "ToList" && m.GetParameters().Count() == 1); MethodInfo genericToListMethod = toListMethod.MakeGenericMethod(new[] { entityInfo.ClrType }); dbSet = genericToListMethod.Invoke(null, new object[] { dbSet }); foreach (var entity in dbSet as System.Collections.IEnumerable) { var entityEntry = dbContext.Entry(entity); object[] row = new object[entityInfo.Fields.Count]; for (int i = 0; i < entityInfo.Fields.Count; i++) { row[i] = entityEntry.Property(entityInfo.Fields[i].Name).CurrentValue; } results.Rows.Add(row); } return(results); } catch (Exception ex) { results.Errors.Add(ex.GetAllMessages()); } return(results); }