private static void SaveUser(UserSaveObject userDTO) { if (string.IsNullOrWhiteSpace(userDTO.Name)) { throw new ArgumentNullException("用户名为空。"); } using (TaskAllocationDBContext ctx = new TaskAllocationDBContext()) { if (userDTO.Id.HasValue) { var user = ctx.Users.Where(u => u.Id == userDTO.Id).FirstOrDefault(); if (user != null) { user.Name = userDTO.Name; } else { throw new ArgumentException("user id is not exist:" + userDTO.Id); } } else { var existCount = ctx.Users.Where(u => u.Name == userDTO.Name).Count(); if (existCount > 0) { throw new ArgumentException("用户名" + userDTO.Name + "已存在。"); } ctx.Users.Add(new User() { Name = userDTO.Name }); } ctx.SaveChanges(); } }
public ActionResult Create(UserSaveObject userDTO) { SaveUser(userDTO); return SuccessMessage(BasicMessage.SubmitSuccess); }