public static bool DeleteTask(TaskWork task) { using (var _context = new DBTaskContext()) { //Lấy thực thể kiểu User từ database var dbTask = (from t in _context.tbTaskWorks where t.ID == task.ID select t).SingleOrDefault(); #region Xoá công việc khỏi quan hệ tbUser foreach (var user in dbTask.listUser) { foreach (var tempTask in user.tasks) { if (tempTask.ID == task.ID) { user.tasks.Remove(tempTask); break; } } } #endregion _context.tbTaskWorks.Remove(dbTask); _context.SaveChanges(); return(true); } }
public static List <TaskWork> GetAllTasks() { using (var _context = new DBTaskContext()) { var dbTask = (from t in _context.tbTaskWorks.Include("listUser").AsEnumerable() select new { ID = t.ID, title = t.title, description = t.description, fromDate = t.fromDate, toDate = t.toDate, listUser = t.listUser }) .Select(x => new TaskWork { ID = x.ID, title = x.title, description = x.description, fromDate = x.fromDate, toDate = x.toDate, listUser = x.listUser }); return(dbTask.ToList()); } }
public static List <User> GetListUsers(string searchUsername) { using (var _context = new DBTaskContext()) { //var user = (from u in _context.tbUsers.AsEnumerable() // where u.username.Contains(searchUsername) // select new // { // username = u.username, // hoTen = u.hoTen, // ngaySinh = u.ngaySinh, // boMon = u.boMon, // soDT = u.soDT, // email = u.email // }) // .Select(x => new User // { // username = x.username, // hoTen = x.hoTen, // ngaySinh = x.ngaySinh, // boMon = x.boMon, // soDT = x.soDT, // email = x.email // }).ToList(); var user = (from u in _context.tbUsers.Include("tasks") where u.username.Contains(searchUsername) select u).ToList(); return(user); } }
public static List <User> GetListUsers() { using (var _context = new DBTaskContext()) { var user = (from u in _context.tbUsers.AsEnumerable() select new { username = u.username, hoTen = u.hoTen, ngaySinh = u.ngaySinh, boMon = u.boMon, soDT = u.soDT, email = u.email }) .Select(x => new User { username = x.username, hoTen = x.hoTen, ngaySinh = x.ngaySinh, boMon = x.boMon, soDT = x.soDT, email = x.email }).ToList(); return(user); } }
//public static bool DeleteUser(string username) //{ // using (var _context = new DBTaskContext()) // { // User user = (from u in _context.tbUsers // where u.username == username // select u).SingleOrDefault(); // _context.tbUsers.Remove(user); // _context.SaveChanges(); // return true; // } //} public static bool DeleteUser(User user) { using (var _context = new DBTaskContext()) { //Lấy thực thể kiểu User từ database var dbUser = (from u in _context.tbUsers where u.username == user.username select u).SingleOrDefault(); #region Xoá user khỏi danh sách công việc foreach (var task in dbUser.tasks) { foreach (var u in task.listUser) { if (u.username == user.username) { task.listUser.Remove(u); break; } } } #endregion _context.tbUsers.Remove(dbUser); _context.SaveChanges(); return(true); } }
public static bool UpdateUser(User user) { using (var _context = new DBTaskContext()) { _context.tbUsers.AddOrUpdate(user); _context.SaveChanges(); return(true); } }
public static void initializeDB() { using (var _context = new DBTaskContext()) { var user = new User { username = "******", ngaySinh = DateTime.Now }; _context.tbUsers.Add(user); _context.SaveChanges(); } MessageBox.Show("Finish!", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information); }
public static int GetIDFromDB() { using (var _context = new DBTaskContext()) { var id = (from t in _context.tbTaskWorks select t.ID).ToList(); if (id.Count <= 0) { return(1); } else { return(id.Max() + 1); } } }
public static bool AddUser(User user) { try { using (var _context = new DBTaskContext()) { _context.tbUsers.Add(user); _context.SaveChanges(); return(true); } } catch { return(false); } }
public static User GetUser(string username) { using (var _context = new DBTaskContext()) { var user = (from u in _context.tbUsers where u.username == username select u).ToList(); if (user.Count == 1) { return(user[0]); } else { return(null); } } }
public static bool UpdateTask(TaskWork task) { using (var _context = new DBTaskContext()) { var dbTask = (from t in _context.tbTaskWorks where t.ID == task.ID select t).SingleOrDefault(); foreach (var user in task.listUser) { var dbUser = (from u in _context.tbUsers where u.username == user.username select u).SingleOrDefault(); dbUser.tasks.Add(dbTask); } task.listUser.Clear(); _context.tbTaskWorks.AddOrUpdate(task); _context.SaveChanges(); return(true); } }
public static bool AddTask(TaskWork task) { using (var _context = new DBTaskContext()) { foreach (var user in task.listUser) { //var dbUser = (from u in _context.tbUsers // where u.username == user.username // select u).Single(); var dbUser = _context.tbUsers.Single(x => x.username == user.username); if (dbUser.tasks is null) { dbUser.tasks = new List <TaskWork>(); } dbUser.tasks.Add(task); } task.listUser.Clear(); _context.tbTaskWorks.AddOrUpdate(task); _context.SaveChanges(); return(true); } }