public ActionResult UserEdit(int id) { User user = null; var userViewData = new UserViewData(); try { using (var db = new TrackerDataContext()) { var options = new DataLoadOptions(); options.LoadWith <Data.User>(u => u.UserRoleList); options.LoadWith <Data.UserRole>(u => u.Role); db.LoadOptions = options; user = db.User.GetByKey(id); UpdateModel(user); if (!db.GetChangeSet().Updates.Contains(user)) { var ruleManager = new RuleManager(); ruleManager.Run(user); if (ruleManager.BrokenRules.Count > 0) { throw new BrokenRuleException(ruleManager.BrokenRules); } } db.SubmitChanges(); var audit = new Audit(db.LastAudit); audit.User = user; db.Audit.InsertOnSubmit(audit); db.SubmitChanges(); } return(RedirectToAction("Edit", new { id = id })); } catch (BrokenRuleException e) { if (user != null) { user.Detach(); } foreach (BrokenRule rule in e.BrokenRules) { ModelState.AddModelError(rule.Context.Rule.TargetProperty, rule.Message); } return(View(GetData(user))); } }
//[Ignore] public void ManyToMany() { var db = new TrackerDataContext { Log = Console.Out }; var user = db.User.GetByKey(1); user.RoleList.Add(db.Role.GetByKey(1)); user.RoleList.Add(db.Role.GetByKey(2)); db.SubmitChanges(); var userRole = user.UserRoleList.FirstOrDefault(u => u.RoleId == 1); db.UserRole.DeleteOnSubmit(userRole); user.RoleList.Remove(db.Role.GetByKey(1)); user.RoleList.Remove(db.Role.GetByKey(2)); var changes = db.GetChangeSet(); db.SubmitChanges(); }