private void MigrateCredentials(User user, List<Credential> creds, string password) { var toRemove = creds.Where(c => !String.Equals( c.Type, CredentialTypes.Password.Pbkdf2, StringComparison.OrdinalIgnoreCase)) .ToList(); // Remove any non PBKDF2 credentials foreach (var cred in toRemove) { creds.Remove(cred); user.Credentials.Remove(cred); CredentialRepository.DeleteOnCommit(cred); } // Now add one if there are no credentials left if (creds.Count == 0) { user.Credentials.Add(CredentialBuilder.CreatePbkdf2Password(password)); } // Save changes, if any (even though we only commit one repo, both will get saved because they just wrap entities context) UserRepository.CommitChanges(); }