public UpdateResult Update(Models.Account account, string lastUpdateUsername) { return(Execute(cx => { Guid findId = account.GetId(); Guid lastUpdateAccountId = cx.Accounts.Single(a => a.Username == lastUpdateUsername).AccountId; Data.Account act = cx.Accounts.SingleOrDefault(a => a.AccountId == findId); if (act == null) { return new UpdateResult("Cannot find account for username: {0}", account.Username); } if (act.LastUpdateTimestamp.Ticks != account.Age) { return new UpdateResult(UpdateResult.ResultType.DataChanged); } try { act.FirstName = account.FirstName; act.LastName = account.LastName; act.EmailAddress = account.EmailAddress; act.PracticeNumber = account.PracticeNumber; act.ProfessionalBodyId = account.ProfessionalBody; act.RegistrationNumber = account.RegistrationNumber; if (!string.IsNullOrEmpty(account.TitleId)) { act.TitleId = Models.BaseModel.DecryptNullableId(account.TitleId); } if (account.AccountRoles != null) { List <Data.AccountRole> accountRoleList = new List <Data.AccountRole>(); if (act.AccountRoles.Count != 0) { accountRoleList = act.AccountRoles.GroupBy(x => x.RoleId).FirstOrDefault().ToList(); } foreach (Data.AccountRole ar in accountRoleList) { act.AccountRoles.Remove(ar); cx.AccountRoles.Remove(ar); } foreach (Data.Models.AccountRole ar in account.AccountRoles) { Guid id = BaseModel.DecryptId(ar.RoleID); if (act.AccountRoles.Where(x => x.RoleId == id).ToList().Count == 0) { act.AccountRoles.Add(new AccountRole() { RoleId = id, AccountId = act.AccountId, AccountRoleId = Guid.NewGuid() }); } } } act.ContactNumber = account.ContactNumber; act.IDNumberType = account.IDType; act.IDNumber = account.IDNumber; if (account.Avatar != null) { act.Avatar = Convert.FromBase64String(account.Avatar.Substring(22)); } if (cx.ChangeTracker.HasChanges()) { WriteAudit(cx, lastUpdateAccountId); act.LastUpdateTimestamp = DateTime.Now; act.LastUpdateAccountId = lastUpdateAccountId; act.FirstSignIn = false; cx.SaveChanges(); } return new UpdateResult(true); } catch (Exception ex) { return new UpdateResult("Error updating profile: {0}", ex.Message); } })); }