public static string ImportUsers(InstagraphContext context, string jsonString) { UserDTO[] UsersFromJSON = JsonConvert.DeserializeObject <UserDTO[]>(jsonString); StringBuilder sb = new StringBuilder(); HashSet <string> usedUsernames = new HashSet <string>(); Dictionary <string, Picture> pictures = context.Pictures.ToDictionary(k => k.Path); foreach (UserDTO userDTO in UsersFromJSON) { bool usernameIsNullOrEmpty = string.IsNullOrWhiteSpace(userDTO.Username); bool passwordIsNullOrEmpty = string.IsNullOrWhiteSpace(userDTO.Password); bool profilePictureIsNullEmpty = string.IsNullOrWhiteSpace(userDTO.ProfilePicture); bool usernameIsNotUnique = true; if (!usernameIsNullOrEmpty) { usernameIsNotUnique = usedUsernames.Contains(userDTO.Username); } bool noSuchProfilePicture = true; if (!profilePictureIsNullEmpty) { noSuchProfilePicture = !pictures.ContainsKey(userDTO.ProfilePicture); } if (!usernameIsNullOrEmpty && !passwordIsNullOrEmpty && !profilePictureIsNullEmpty && !usernameIsNotUnique && !noSuchProfilePicture) { User newUser = new User() { Username = userDTO.Username, Password = userDTO.Password, ProfilePictureId = pictures[userDTO.ProfilePicture].Id, ProfilePicture = pictures[userDTO.ProfilePicture] }; context.Add(newUser); sb.AppendLine($"Successfully imported User {newUser.Username}."); usedUsernames.Add(newUser.Username); } else { sb.AppendLine("Error: Invalid data."); } } context.SaveChanges(); string result = sb.ToString(); return(result); }