public static void SendUnreadPrivateMessageToClient(object data) { int userId = (int)data; ClientInfo clientTo = listClients.FirstOrDefault(x => x.UserId == userId); using (DataModelContainer db = new DataModelContainer()) { int[] messIds = db.ReadMessages.Where(x => x.User_Id == userId && x.IsRead == false).Select(x => x.Message_Id).ToArray(); var unreadMess = db.Messages.Where(x => (x.UserTo_Id == userId && x.IsRead == false) || messIds.Contains(x.Id)).OrderBy(x => x.Date); // все непрочитанные сообщения, личные и групповые foreach (var mess in unreadMess) { if (mess.UserTo_Id != null) //личное сообщение { bool res = clientTo.callback.ReceivePrivateMessage(GetLoginFrom(mess), GetLoginTo(mess), mess.Text, mess.Date); if (res) { mess.IsRead = true; db.Entry(mess).Property(c => c.IsRead).IsModified = true; } } else //групповое сообщение { bool res = clientTo.callback.ReceivePrivateMessage(GetLoginFrom(mess), GetGroupTo(mess), mess.Text, mess.Date); if (res) { ReadMessage rm = db.ReadMessages.FirstOrDefault(x => x.Message_Id == mess.Id && x.User_Id == userId); rm.IsRead = true; db.Entry(rm).Property(c => c.IsRead).IsModified = true; } } } db.SaveChanges(); } }
public ActionResult Delete(int bId) { Bill bill = dbContext.Bill.Find(bId); dbContext.Entry(bill).State = System.Data.Entity.EntityState.Deleted; dbContext.SaveChanges(); return(RedirectToAction("Index", new { pageIndex = Session["pageIndex"] })); }
public ActionResult DeleteUser(int userId) { UserInfo userInfo = dbContext.UserInfo.Find(userId); dbContext.Entry(userInfo).State = System.Data.Entity.EntityState.Deleted; dbContext.SaveChanges(); return(RedirectToAction("Index", new { pageIndex = Session["pageIndex"] })); }
public ActionResult Delete(int proId) { Provider provider = dbContext.Provider.Find(proId); dbContext.Entry(provider).State = System.Data.Entity.EntityState.Deleted; dbContext.SaveChanges(); return(RedirectToAction("Index", new { pageIndex = Session["pageIndex"] })); }
public ActionResult Edit(int id, UserInfo userInfo) { try { // TODO: Add update logic here dbContext.Entry(userInfo).State = System.Data.Entity.EntityState.Modified; dbContext.SaveChanges(); return(RedirectToAction("Index")); } catch { return(View()); } }
public int addRate(int userId, int movieId, int rateValue) { using (var db = new DataModelContainer()) { db.Database.Connection.Open(); var myRate = new Rate() { rate = rateValue, UserId = userId, MovieId = movieId }; db.Entry(myRate).State = System.Data.Entity.EntityState.Added; int res = db.SaveChanges(); if (res > 0) { MessageBox.Show("successfuly add rate"); return(res); } else { MessageBox.Show("Error can't add rate to the movie"); return(res); } } }
public int createComment(int userId, int movieId, string commentContent) { using (var db = new DataModelContainer()) { db.Database.Connection.Open(); var myComment = new Comment() { content = commentContent, UserId = userId, MovieId = movieId }; db.Entry(myComment).State = System.Data.Entity.EntityState.Added; int res = db.SaveChanges(); if (res > 0) { MessageBox.Show("Comment creation successful"); return(res); } else { MessageBox.Show("Error cant create comment"); return(res); } } }
public int ModifyPwd(int id, string newPwd) { #region 方法1 //UserInfo user = db.UserInfo.Where(u => u.Id == id).FirstOrDefault(); //user.Pwd = newPwd; //return db.SaveChanges(); #endregion #region 方法2 //UserInfo user = new UserInfo(); //user.Id = id; //user.Pwd = newPwd; //db.UserInfo.Attach(user); //相当于下面一句 //db.Entry<UserInfo>(user).Property<string>(u => u.Pwd).IsModified = true; //db.Configuration.ValidateOnSaveEnabled = false;//关闭自动验证实体 //return db.SaveChanges(); #endregion #region 方法3 UserInfo user = db.UserInfo.Find(id); //user.Id = id; user.Pwd = newPwd; db.UserInfo.Attach(user); //相当于下面一句 db.Entry <UserInfo>(user).Property <string>(u => u.Pwd).IsModified = true; db.Configuration.ValidateOnSaveEnabled = false; //关闭自动验证实体 return(db.SaveChanges()); #endregion }
// 实现对数据库的添加功能,添加实现EF框架的引用 public T AddEntity(T entity) { //EF4.0的写法 添加实体 //db.CreateObjectSet<T>().AddObject(entity); //EF5.0的写法 db.Entry <T>(entity).State = EntityState.Added; //下面的写法统一 db.SaveChanges(); return(entity); }
public virtual async Task <CommandResult <UserProfileData> > Create(UserProfileData data) { var entity = data.ToEntity(); _context.Set(entity.GetType()).Attach(entity); _context.Entry(entity).State = System.Data.Entity.EntityState.Added; await _context.SaveChangesAsync(); return(new CommandResult <UserProfileData>(data, new UserProfileData(entity))); }
public ActionResult Edit([Bind(Include = "Id,TotalPrice,Tip,State")] Order order) { if (ModelState.IsValid) { db.Entry(order).State = EntityState.Modified; db.SaveChanges(); return(RedirectToAction("Index")); } return(View(order)); }
public virtual async Task <CommandResult <UserData> > Update(UserData data) { var entity = data.ToEntity(); _context.Set(entity.GetType()).Attach(entity); _context.Entry(entity).State = System.Data.Entity.EntityState.Modified; var rows = await _context.SaveChangesAsync(); return(new CommandResult <UserData>(data, new UserData(entity), rows > 0)); }
public virtual async Task <CommandResult <RoleData> > Delete(RoleData data) { var entity = data.ToEntity(); _context.Set(entity.GetType()).Attach(entity); _context.Entry(entity).State = System.Data.Entity.EntityState.Deleted; var rows = await _context.SaveChangesAsync(); return(new CommandResult <RoleData>(data, null, rows > 0)); }
public ActionResult Index(string NewPassword) { string LoginName = Session["userPhone"] == null ? Session["userName"].ToString() : Session["userPhone"].ToString(); if (Session["userPhone"] == null) { var admin = dbContext.AdminInfo.Where(a => a.AdminName == LoginName).FirstOrDefault(); //int id = admin.Id; admin.AdminPwd = NewPassword; dbContext.Entry(admin).State = System.Data.Entity.EntityState.Modified; dbContext.SaveChanges(); } else { var user = dbContext.UserInfo.Where(u => u.UPhone == LoginName).FirstOrDefault(); user.UPwd = NewPassword; dbContext.Entry(user).State = System.Data.Entity.EntityState.Modified; dbContext.SaveChanges(); } return(RedirectToAction("Index", "Index")); }
public static void SendPrivateMessageToClients(object data) { Message mess = (Message)data; if (mess.UserTo_Id != null) // шлем личное сообщение пользователю { ClientInfo clientTo = listClients.FirstOrDefault(x => x.UserId == mess.UserTo_Id); if (clientTo != null) { bool res = clientTo.callback.ReceivePrivateMessage(GetLoginFrom(mess), GetLoginTo(mess), mess.Text, mess.Date); if (res) { using (var db = new DataModelContainer()) { Message message = db.Messages.FirstOrDefault(x => x.Id == mess.Id); message.IsRead = true; db.Entry(message).State = System.Data.Entity.EntityState.Modified; db.SaveChanges(); } } } ClientInfo clientFrom = listClients.FirstOrDefault(x => x.UserId == mess.UserFrom_Id); if (clientFrom != null && clientFrom != clientTo) { clientFrom.callback.ReceivePrivateMessage(GetLoginFrom(mess), GetLoginTo(mess), mess.Text, mess.Date); } } else if (mess.GroupTo_Id != null) //шлем групповое сообщение { using (var db = new DataModelContainer()) { Group grp = db.Groups.FirstOrDefault(x => x.Id == mess.GroupTo_Id); foreach (var us in grp.User) { ClientInfo client = listClients.FirstOrDefault(x => x.UserId == us.Id); if (client != null) { bool res = client.callback.ReceivePrivateMessage(GetLoginFrom(mess), GetGroupTo(mess), mess.Text, mess.Date); if (res) { ReadMessage rm = db.ReadMessages.FirstOrDefault(x => x.Message_Id == mess.Id && x.User_Id == client.UserId); rm.IsRead = true; db.Entry(rm).State = System.Data.Entity.EntityState.Modified; } } } db.SaveChanges(); } } }
/// <summary> /// 批量更改 數據 /// </summary> /// <param name="ID">工號數組</param> /// <param name="state">出勤狀態數組</param> /// <param name="update">更新字段</param> /// <returns>更改數據的數量</returns> public int update_Attendance(List <string> ID, List <string> state, string update) { DateTime now = DateTime.Now; string today = now.Date.ToShortDateString(); DateTime start = Convert.ToDateTime(today.Trim() + " " + "00:00:00"); DateTime end = Convert.ToDateTime(today.Trim() + " " + "23:59:59"); int count = 0; foreach (string id in ID) { var temp = dbContext.Attendance.FirstOrDefault(u => u.ID == id && u.date1 > start && u.date1 < end); { string state1 = temp.state1; string state2 = temp.state2; Attendance newad = new Attendance(); newad = temp; switch (update) { case "state1": newad.state1 = state[count]; newad.date1 = DateTime.Now; if (state[count] != state1) { newad.reason1 = null; newad.note1 = null; newad.reason2 = null; newad.state2 = null; } break; case "reason1": newad.reason1 = state[count]; newad.date1 = DateTime.Now; break; case "state2": newad.state2 = state[count]; newad.date2 = DateTime.Now; if (state[count] != state2) { newad.reason2 = null; newad.note2 = null; } break; case "reason2": newad.reason2 = state[count]; newad.date2 = DateTime.Now; break; case "time1": newad.time1 = double.Parse(state[count]); break; } dbContext.Entry(temp).CurrentValues.SetValues(newad);; } count++; } return(dbContext.SaveChanges()); }
public ActionResult Update([DataSourceRequest] DataSourceRequest request, VideoViewModel video) { if (video != null && ModelState.IsValid) { Video modelVideo = video.ToVideo(); db.Entry(modelVideo).State = EntityState.Modified; try { db.SaveChanges(); } catch (Exception ex) { ModelState.AddModelError("", ex.Message); } return(Json(new[] { VideoViewModel.From(modelVideo) }.ToDataSourceResult(request, ModelState))); } return(Json(new[] { video }.ToDataSourceResult(request, ModelState))); }
public bool deleteCommentsByUser(int userId) { using (var db = new DataModelContainer()) { db.Database.Connection.Open(); var comments = (from r in db.CommentSet where r.UserId == userId select r).ToList(); if (comments.Count < 1) { return(false); } comments.ForEach(delegate(Comment comment) { db.Entry(comment).State = System.Data.Entity.EntityState.Deleted; }); return(db.SaveChanges() != 0); } }
public int UpdateClass(List <string> ID) { if (ID == null) { return(0); } foreach (string id in ID) { //需要修改的實體 var staff = dbContext.Staff.FirstOrDefault(m => m.ID == id); if (staff != null) { Staff newstaff = new Staff(); newstaff = staff; newstaff.CLASS = staff.CLASS == "白班" ? "晚班" : "白班"; //換班 dbContext.Entry(staff).CurrentValues.SetValues(newstaff); } } return(dbContext.SaveChanges()); }
/// <summary> /// /// </summary> /// <param name="item"></param> /// <returns>A saved entity, otherwise, same passed object.</returns> public T Add(T item) { try { Context.Set <T>().Attach(item); Context.Entry(item).State = EntityState.Added; Context.SecureSaveChanges(); } catch (Exception ex) { var dataException = DataLayerExceptionFactory.Create(ex, item); throw dataException; } return(item); }