public static T Find(params object[] keyValues) { using (MVCDemoDBEntities db = new MVCDemoDBEntities()) { return(db.Set <T>().Find(keyValues)); } }
public List <T> FindAll() { using (MVCDemoDBEntities db = new MVCDemoDBEntities()) { return(db.Set <T>().ToList()); } }
public static void Delete(T model) { using (MVCDemoDBEntities db = new MVCDemoDBEntities()) { db.Set <T>().Remove(model); db.SaveChanges(); } }
public static void AddAdmin(Admin admin) { using (MVCDemoDBEntities db = new MVCDemoDBEntities()) { db.Admin.Add(admin); db.SaveChanges(); } }
public static List <T> Search <TKey>(Expression <Func <T, bool> > expression, Expression <Func <T, TKey> > orderBy, int pageIndex, int pageSize, out int totalCount) { using (MVCDemoDBEntities db = new MVCDemoDBEntities()) { totalCount = db.Set <T>().Where(expression).Count(); return(db.Set <T>().Where(expression).OrderByDescending(orderBy).Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList()); } }
public static List <Admin> SearchByConditions(Expression <Func <Admin, bool> > expression, int pageIndex, int pageSize, out int totalCount) { using (MVCDemoDBEntities db = new MVCDemoDBEntities()) { totalCount = db.Admin.Where(expression).Count(); return(db.Admin.Where(expression).OrderByDescending(i => i.CreatedTime).Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList()); } }
public static T Add(T model) { using (MVCDemoDBEntities db = new MVCDemoDBEntities()) { db.Set <T>().Add(model); db.SaveChanges(); } return(model); }
public static void EditAdmin(Admin admin) { using (MVCDemoDBEntities db = new MVCDemoDBEntities()) { db.Set <Admin>().Attach(admin); db.Entry <Admin>(admin).State = EntityState.Modified; db.SaveChanges(); } }
public static Admin GetAdminByID(int id) { Admin admin; using (MVCDemoDBEntities db = new MVCDemoDBEntities()) { admin = db.Admin.Find(id); } return(admin); }
/// <summary> /// 根据类型查询字典 /// </summary> /// <param name="type"></param> /// <returns></returns> public static List <Dictionary> GetDictionaryByType(string type) { if (!string.IsNullOrEmpty(type)) { using (MVCDemoDBEntities db = new MVCDemoDBEntities()) { List <Dictionary> dictionarys = db.Dictionary.Where(m => m.Type == type && m.IsDeleted == false).ToList(); return(dictionarys); } } return(null); }
public static void Delete(params object[] keyValues) { using (MVCDemoDBEntities db = new MVCDemoDBEntities()) { T model = db.Set <T>().Find(keyValues); if (model != null) { db.Set <T>().Remove(model); db.SaveChanges(); } } }
public static void DeleteLogical(int id) { using (MVCDemoDBEntities db = new MVCDemoDBEntities()) { Role role = db.Role.Find(id); if (role != null) { role.IsDeleted = true; db.SaveChanges(); } } }
public static void DeleteAdmin(int id) { using (MVCDemoDBEntities db = new MVCDemoDBEntities()) { Admin admin = db.Admin.Find(id); if (admin != null) { admin.IsDeleted = true; } db.SaveChanges(); } }
public static T Update(T model) { using (MVCDemoDBEntities db = new MVCDemoDBEntities()) { if (db.Entry <T>(model).State == EntityState.Modified) { db.SaveChanges(); } else if (db.Entry <T>(model).State == EntityState.Detached) { db.Set <T>().Attach(model); db.Entry <T>(model).State = EntityState.Modified; db.SaveChanges(); } } return(model); }