public PreferenceModel() { db = DataContext.getUasDbContext(); string loginId = SessionHelper.LoginInfo.LoginID; //context.Configuration.ValidateOnSaveEnabled = false; user = db.uas_User.Where(u => u.LoginID == loginId).FirstOrDefault(); phone = user.uas_UserPhone.FirstOrDefault(); if (phone == null) { phone = new uas_UserPhone(); phone.UserID = user.UserID; phone.uas_User = user; phone.PhoneNumber = String.Empty; phone.PhoneType = "Primary"; phone.SortOrder = 1; phone.StatusFlag = "A"; phone.CreatedDate = DateTime.Now; phone.CreatedBy = SessionHelper.LoginStatus.UserID; db.uas_UserPhone.Add(phone); db.SaveChanges(); phone = user.uas_UserPhone.FirstOrDefault(); } address = user.uas_UserAddress.FirstOrDefault(); if (address == null) { address = new uas_UserAddress(); address.UserID = user.UserID; address.uas_User = user; address.AddressType = "Primary"; address.Address1 = String.Empty; address.Address2 = String.Empty; address.City = String.Empty; address.StateProvince = String.Empty; address.SortOrder = 1; address.StatusFlag = "A"; address.CreatedDate = DateTime.Now; address.CreatedBy = SessionHelper.LoginStatus.UserID; db.uas_UserAddress.Add(address); db.SaveChanges(); address = user.uas_UserAddress.FirstOrDefault(); } email = user.uas_UserEmail.FirstOrDefault(); if (email == null) { email = new uas_UserEmail(); email.UserID = user.UserID; email.uas_User = user; email.EmailType = "Primary"; email.EmailAddress = String.Empty; email.SortOrder = 1; email.StatusFlag = "A"; email.CreatedDate = DateTime.Now; email.CreatedBy = SessionHelper.LoginStatus.UserID; db.uas_UserEmail.Add(email); db.SaveChanges(); email = user.uas_UserEmail.FirstOrDefault(); } enterprise = user.uas_Enterprise; }
public string Save(PreferenceModel pref) { Debug.WriteLine("PreferencesController.Save from form - first last:" + pref.user.FirstName + " " + pref.user.LastName); Debug.WriteLine("PreferencesController.Save address: " + pref.address.Address1); if (!SessionHelper.IsUserLoggedIn) { return("User not logged in"); } uas_User user = pref.db.uas_User.Where(u => u.UserID == SessionHelper.LoginStatus.UserID).FirstOrDefault(); uas_UserPhone userPhone = pref.db.uas_UserPhone.Where(u => u.UserID == SessionHelper.LoginStatus.UserID).FirstOrDefault(); uas_UserAddress userAddress = pref.db.uas_UserAddress.Where(u => u.UserID == SessionHelper.LoginStatus.UserID).FirstOrDefault(); uas_UserEmail userEmail = pref.db.uas_UserEmail.Where(u => u.UserID == SessionHelper.LoginStatus.UserID).FirstOrDefault(); user.FirstName = pref.user.FirstName; user.LastName = pref.user.LastName; user.MiddleName = pref.user.MiddleName; user.Title = pref.user.Title; user.Area = pref.user.Area; userPhone.PhoneNumber = pref.phone.PhoneNumber; userPhone.Extension = pref.phone.Extension; userAddress.Address1 = pref.address.Address1; userAddress.City = pref.address.City; userAddress.StateProvince = pref.address.StateProvince; userAddress.PostalCode = pref.address.PostalCode; userEmail.EmailAddress = pref.email.EmailAddress; user.ModifiedBy = SessionHelper.LoginStatus.UserID; user.ModifiedDate = DateTime.Now; userPhone.ModifiedBy = SessionHelper.LoginStatus.UserID; userPhone.ModifiedDate = DateTime.Now; userAddress.ModifiedBy = SessionHelper.LoginStatus.UserID; userAddress.ModifiedDate = DateTime.Now; userEmail.ModifiedBy = SessionHelper.LoginStatus.UserID; userEmail.ModifiedDate = DateTime.Now; pref.db.Entry(user).State = System.Data.Entity.EntityState.Modified; pref.db.Entry(userAddress).State = System.Data.Entity.EntityState.Modified; pref.db.Entry(userPhone).State = System.Data.Entity.EntityState.Modified; pref.db.Entry(userEmail).State = System.Data.Entity.EntityState.Modified; pref.user = user; pref.address = userAddress; pref.phone = userPhone; pref.email = userEmail; try { pref.db.SaveChanges(); } catch (DbEntityValidationException dbEx) { Debug.WriteLine("Save DbEntityValidation Exception: "); string errors = ""; foreach (DbEntityValidationResult devr in dbEx.EntityValidationErrors) { foreach (DbValidationError dve in devr.ValidationErrors) { Debug.WriteLine(" DbEntityValidationResult: " + dve.ErrorMessage); errors += dve.ErrorMessage + "<br />"; } } return(errors); } catch (System.Data.DataException de) { Debug.WriteLine("Save DataException: " + de.Message); return(de.Message); } catch (Exception xcptn) { Debug.WriteLine("Save Exception: " + xcptn.Message); return(xcptn.Message); } return("success"); }