Ejemplo n.º 1
0
 /// <summary>
 /// 按条件删除数据
 /// </summary>
 /// <typeparam name="T">表的实体类</typeparam>
 /// <param name="where">linq</param>
 /// <returns>删除行数</returns>
 public static int Delete <T>(Expression <Func <T, bool> > where) where T : class
 {
     try
     {
         if (where == null)
         {
             throw new ArgumentNullException();
         }
         int rows = 0;
         using (var db = new DemoEntities())
         {
             var list = db.Set <T>().Where(where).ToList();
             foreach (var item in list)
             {
                 db.Set <T>().Remove(item);
                 rows += db.SaveChanges();
             }
         }
         return(rows);
     }
     catch (Exception ex)
     {
         WriteException(ex);
         return(0);
     }
 }
Ejemplo n.º 2
0
 /// <summary>
 /// 更新指定字段
 /// </summary>
 /// <param name="model">实体</param>
 /// <param name="fileds">更新字段数组</param>
 public static int Update <T>(T model, params string[] fileds) where T : class
 {
     try
     {
         if (model == null || fileds == null)
         {
             return(0);
         }
         using (var db = new DemoEntities())
         {
             db.Set <T>().Attach(model);
             var SetEntry = ((IObjectContextAdapter)db).ObjectContext.ObjectStateManager.GetObjectStateEntry(model);
             foreach (var filed in fileds)
             {
                 SetEntry.SetModifiedProperty(filed);
             }
             return(db.SaveChanges());
         }
     }
     catch (Exception ex)
     {
         WriteException(ex);
         return(0);
     }
 }
Ejemplo n.º 3
0
        /// <summary>
        /// 根据条件分页查询
        /// </summary>
        /// <typeparam name="T">表的实体类</typeparam>
        /// <param name="page">页码</param>
        /// <param name="size">页面大小</param>
        /// <param name="where">linq表达式</param>
        /// <returns></returns>
        public static DataGrid <T> SelectPage <T, TKey>(int page, int size, Expression <Func <T, TKey> > orderby, Expression <Func <T, bool> > where = null) where T : class
        {
            try
            {
                where = where ?? (p => true);
                var total = Count(where);
                if (total <= 0)
                {
                    return new DataGrid <T>()
                           {
                               total = 0, rows = new List <T>(), emptyMsg = "没有查询到相关数据"
                           }
                }
                ;
                DataGrid <T> grid = new DataGrid <T>();
                grid.total    = total;
                grid.emptyMsg = "查询成功";

                using (var db = new DemoEntities())
                {
                    grid.rows = db.Set <T>().Where(where).OrderBy(orderby).Skip((page - 1) * size).Take(size).ToList();
                }
                return(grid);
            }
            catch (Exception ex)
            {
                WriteException(ex);
                return(new DataGrid <T>()
                {
                    total = 0, rows = new List <T>(), emptyMsg = "查询故障,请联系开发人员"
                });
            }
        }
Ejemplo n.º 4
0
 /// <summary>
 /// 根据条件查询数据量,返回-1表示错误
 /// </summary>
 /// <typeparam name="T">表的实体类</typeparam>
 /// <param name="where">linq表达式</param>
 public static int Count <T>(Expression <Func <T, bool> > where) where T : class
 {
     try
     {
         where = where ?? (p => true);
         using (var db = new DemoEntities())
         {
             return(db.Set <T>().Count(where));
         }
     }
     catch (Exception ex)
     {
         WriteException(ex);
         return(-1);
     }
 }
Ejemplo n.º 5
0
 /// <summary>
 /// 根据条件查询数据
 /// </summary>
 /// <typeparam name="T">表的实体类</typeparam>
 /// <param name="where">linq表达式</param>
 public static IList <T> Select <T>(Expression <Func <T, bool> > where = null) where T : class
 {
     try
     {
         where = where ?? (p => true);
         using (var db = new DemoEntities())
         {
             return(db.Set <T>().Where(where).ToList());
         }
     }
     catch (Exception ex)
     {
         WriteException(ex);
         return(new List <T>());
     }
 }
Ejemplo n.º 6
0
 /// <summary>
 /// 新增多条数据
 /// </summary>
 /// <typeparam name="T">表的实体类</typeparam>
 /// <param name="models">多条模型数据</param>
 public static int InsertList <T>(IEnumerable <T> models) where T : class
 {
     try
     {
         int rows = 0;
         using (var db = new DemoEntities())
         {
             db.Set <T>().AddRange(models);
             rows += db.SaveChanges();
         }
         return(rows);
     }
     catch (Exception ex)
     {
         WriteException(ex);
         return(0);
     }
 }
Ejemplo n.º 7
0
 /// <summary>
 /// 更新单条数据
 /// </summary>
 /// <typeparam name="T">表的实体类</typeparam>
 /// <param name="model">模型数据</param>
 public static int Update <T>(T model) where T : class
 {
     try
     {
         int rows = 0;
         using (var db = new DemoEntities())
         {
             if (db.Entry(model).State == EntityState.Detached)
             {
                 db.Set <T>().Attach(model);
                 db.Entry(model).State = EntityState.Modified;
             }
             rows += db.SaveChanges();
         }
         return(rows);
     }
     catch (Exception ex)
     {
         WriteException(ex);
         return(0);
     }
 }
Ejemplo n.º 8
0
 /// <summary>
 /// 使用查询条件更新数据
 /// </summary>
 /// <typeparam name="T">表的实体类</typeparam>
 /// <param name="action">更新数据动作</param>
 /// <param name="where">表达式</param>
 public static int Update <T>(Action <T> action, Expression <Func <T, bool> > where) where T : class
 {
     try
     {
         int rows = 0;
         using (var db = new DemoEntities())
         {
             var list = db.Set <T>().Where(where);
             foreach (var model in list)
             {
                 action.Invoke(model);
                 db.Entry(model).State = EntityState.Modified;
             }
             rows += db.SaveChanges();
         }
         return(0);
     }
     catch (Exception ex)
     {
         WriteException(ex);
         return(0);
     }
 }