public void Test() { var u = new User() { Uid = "001", Email = null, Name = "a" }; Db.AddUser(u); Assert.True(Db.Users.FirstOrDefault(x => x.Uid == u.Uid) != null); Assert.True(Db.FindUser("001") != null); u.Name = "b"; Db.UpdateUser(u); Assert.True(Db.FindUser("001").Name == "b"); Db.DeleteUser(u); Assert.True(Db.FindUser("001") == null); }
public ActionResult UpdateUser(UserRegister data, string updateType) { if (string.IsNullOrEmpty(updateType)) { updateType = "1"; } UserDbContext uc = new UserDbContext("ConnStr1"); Users data2 = null; try { switch (updateType) { case "1": if (data != null) { data2 = (Users)data; data = (UserRegister)uc.AddUser(data2); uc.SaveChanges(); } else { data = new UserRegister(); } break; case "2": if (data == null) { throw new Exception("数据为空,无法更新。"); } uc.ModifyUser(data); uc.SaveChanges(); break; case "3": if (data == null) { throw new Exception("数据为空,无法删除。"); } uc.DeleteUser(data); uc.SaveChanges(); break; default: throw new Exception("更新类型出错。"); } string script = MvcUT.ShowAlertAndHref(this, "更新成功", "UpdateUser"); return(Content(script)); } catch (Exception ex) { if (ex.Message != null) { } this.ViewBag.ErrMsg = MisBase.BaseUT.GetRootException(ex).Message; return(View(data)); } }