public void UpdateRoles(string aspNetUserId, string[] roles) { using (var db = new EntityContext()) { try { var existRoles = db.AspNetUserRoles.Where(x => x.UserId == aspNetUserId.ToString()); var oldRoles = existRoles.Select(x => x.RoleId).ToArray(); if (roles == null) { roles = new string[] { } } ; //Add New Roles Selected roles.Except(oldRoles).ToList().ForEach(x => { var userRole = new EM.AspNetUserRole { UserId = aspNetUserId, RoleId = x, Created = DateTime.Now }; db.Entry(userRole).State = System.Data.Entity.EntityState.Added; }); //Delete existing Roles unselected existRoles.Where(x => !roles.Contains(x.RoleId)).ToList().ForEach(x => { db.Entry(x).State = System.Data.Entity.EntityState.Deleted; }); db.SaveChanges(); } catch (Exception ex) { LoggingHelper.LogError(ex, thisClassName + string.Format(".UpdateRoles(), aspNetUserId: {0}", aspNetUserId)); //statusMessage = ex.Message; } } }
public bool Update(AppUser entity, ref string statusMessage) { using (var context = new EntityContext()) { try { EM.Account efEntity = context.Accounts .SingleOrDefault(s => s.Id == entity.Id); if (efEntity != null && efEntity.Id > 0) { AppUser_FromMap(entity, efEntity); context.Entry(efEntity).State = System.Data.Entity.EntityState.Modified; if (HasStateChanged(context)) { efEntity.LastUpdated = System.DateTime.Now; // submit the change to database int count = context.SaveChanges(); if (count > 0) { statusMessage = "successful"; //arbitrarily update AspNetUsers??? AspNetUsers_Update(entity, ref statusMessage); return(true); } else { //?no info on error statusMessage = "Error - the update was not successful. "; string message = string.Format(thisClassName + ".Account_Update Failed", "Attempted to uddate a Account. The process appeared to not work, but was not an exception, so we have no message, or no clue.Email: {0}", entity.Email); EmailManager.NotifyAdmin(thisClassName + ". Account_Update Failed", message); } } } } catch (Exception ex) { LoggingHelper.LogError(ex, thisClassName + string.Format(".Account_Update(), Email: {0}", entity.Email)); } } return(false); }