Beispiel #1
0
 public async Task <T> GetEntityAsync(Expression <Func <T, bool> > where, string connectionString = null)
 {
     using (SystemContext context = DataBaseConfig.CreateContext(connectionString))
     {
         return(await context.Set <T>().FirstOrDefaultAsync(where));
     }
 }
Beispiel #2
0
 public async Task <IEnumerable <T> > GetListAsync(Expression <Func <T, bool> > where, string connectionString = null)
 {
     using (SystemContext context = DataBaseConfig.CreateContext(connectionString))
     {
         return(await context.Set <T>().AsNoTracking().Where(where).ToListAsync());
     }
 }
Beispiel #3
0
 public async Task <IEnumerable <T> > GetListAsync(string connectionString = null)
 {
     using (SystemContext context = DataBaseConfig.CreateContext(connectionString))
     {
         return(await context.Set <T>().AsNoTracking().ToListAsync());
     }
 }
Beispiel #4
0
 public async Task <T> GetEntityAsync(string keyValue, string connectionString = null)
 {
     using (SystemContext context = DataBaseConfig.CreateContext(connectionString))
     {
         return(await context.FindAsync <T>(keyValue));
     }
 }
Beispiel #5
0
 public async Task <bool> DeleteAsync(IEnumerable <T> entityList, string connectionString = null)
 {
     using (SystemContext context = DataBaseConfig.CreateContext(connectionString))
     {
         context.Set <T>().RemoveRange(entityList);
         return(await context.SaveChangesAsync() > 0);
     }
 }
Beispiel #6
0
 public async Task <bool> DeleteAsync(string keyValue, string connectionString = null)
 {
     using (SystemContext context = DataBaseConfig.CreateContext(connectionString))
     {
         context.Set <T>().Remove(context.Find <T>(keyValue));
         return(await context.SaveChangesAsync() > 0);
     }
 }
Beispiel #7
0
 public async Task <bool> UpdateAsync(T entity, string connectionString = null)
 {
     using (SystemContext context = DataBaseConfig.CreateContext(connectionString))
     {
         context.Update(entity);
         return(await context.SaveChangesAsync() > 0);
     }
 }
Beispiel #8
0
 public async Task <bool> DeleteAsync(string[] keyValues, string connectionString = null)
 {
     using (SystemContext context = DataBaseConfig.CreateContext(connectionString))
     {
         List <T> entitys = new List <T>();
         entitys.ForEach(async u =>
         {
             entitys.Add(await context.FindAsync <T>(u));
         });
         return(await DeleteAsync(entitys));
     }
 }
Beispiel #9
0
        //public async  Task<Tuple<IEnumerable<T>, int>> GetListAsync(Expression<Func<T, bool>> where, string orderField, bool isAsc, int pageSize, int pageIndex, string connectionString = null)
        //{
        //    using (SystemContext context = DataBaseConfig.CreateContext(connectionString))
        //    {
        //        var tempData = context.Set<T>().AsQueryable();
        //       int total =await tempData.CountAsync();
        //        if (!string.IsNullOrWhiteSpace(orderField))
        //        {
        //            string[] _order = orderField.Split(",");
        //            MethodCallExpression resultExp = null;
        //            foreach (string item in _order)
        //            {
        //                string _orderPart = item;
        //                _orderPart = Regex.Replace(_orderPart, @"\s+", " ");
        //                string[] _orderArry = _orderPart.Split(' ');
        //                string _orderField = _orderArry[0];
        //                bool sort = isAsc;
        //                if (_orderArry.Length == 2)
        //                {
        //                    isAsc = _orderArry[1].ToUpper() == "ASC" ? true : false;
        //                }
        //                var parameter = Expression.Parameter(typeof(T), "t");
        //                var property = typeof(T).GetProperty(_orderField);
        //                var propertyAccess = Expression.MakeMemberAccess(parameter, property);
        //                var orderByExp = Expression.Lambda(propertyAccess, parameter);
        //                resultExp = Expression.Call(typeof(Queryable), isAsc ? "OrderBy" : "OrderByDescending", new Type[] { typeof(T), property.PropertyType }, tempData.Expression, Expression.Quote(orderByExp));
        //            }
        //            tempData = tempData.Provider.CreateQuery<T>(resultExp);
        //        }
        //        else
        //        {
        //            tempData = tempData.Skip(pageSize * (pageIndex - 1)).Take(pageSize).AsQueryable();
        //        }
        //        return new Tuple<IEnumerable<T>, int>(tempData.AsNoTracking(), total);
        //    }
        //}
        public async Task <Tuple <IEnumerable <T>, int> > GetListAsync <Tkey>(Expression <Func <T, bool> > where, Func <T, Tkey> orderbyLambda, bool isAsc, int pageSize, int pageIndex, string connectionString = null)
        {
            using (SystemContext context = DataBaseConfig.CreateContext(connectionString))
            {
                int total = await context.Set <T>().Where(where).AsNoTracking().CountAsync();

                if (isAsc)
                {
                    var temp = context.Set <T>().Where(where)
                               .OrderBy(orderbyLambda)
                               .Skip(pageSize * (pageIndex - 1))
                               .Take(pageSize).ToList();
                    return(new Tuple <IEnumerable <T>, int>(temp, total));
                }
                else
                {
                    var temp = context.Set <T>().Where(where)
                               .OrderByDescending(orderbyLambda)
                               .Skip(pageSize * (pageIndex - 1))
                               .Take(pageSize);
                    return(new Tuple <IEnumerable <T>, int>(temp, total));
                }
            }
        }