public IActionResult AddUserMinistryPreference([FromQuery] string[] ministryKeys) { try { var header = Request.Headers["Authorization"]; var email = GetEmailAddressFromAuthorizationHeader(Request.Headers["Authorization"]); var dbUserMinistryPrefs = dbContext.UserMinistryPreference.Include(m => m.Ministry).Where(p => p.Email == email).ToList(); dbContext.RemoveRange(dbUserMinistryPrefs); var userPrefs = new List <UserMinistryPreference>(); foreach (var key in ministryKeys) { var ministry = dbContext.Ministry.SingleOrDefault(m => m.Key == key); userPrefs.Add(new UserMinistryPreference { Email = email, Ministry = ministry }); } dbContext.AddRange(userPrefs); dbContext.SaveChanges(); return(CreatedAtRoute("GetUserMinistryPreferences", ministryKeys)); } catch (Exception ex) { return(BadRequest("Failed to create user ministry preferences", ex)); } }