diff --git a/bitwarden_license/src/Commercial.Infrastructure.EntityFramework/SecretsManager/Repositories/SecretRepository.cs b/bitwarden_license/src/Commercial.Infrastructure.EntityFramework/SecretsManager/Repositories/SecretRepository.cs index 1bd02992c3..e2226c3038 100644 --- a/bitwarden_license/src/Commercial.Infrastructure.EntityFramework/SecretsManager/Repositories/SecretRepository.cs +++ b/bitwarden_license/src/Commercial.Infrastructure.EntityFramework/SecretsManager/Repositories/SecretRepository.cs @@ -154,41 +154,32 @@ public class SecretRepository : Repository> ImportAsync(IEnumerable secrets) { - try + using (var scope = ServiceScopeFactory.CreateScope()) { - using (var scope = ServiceScopeFactory.CreateScope()) + var dbContext = GetDatabaseContext(scope); + var entities = new List(); + var projects = secrets + .SelectMany(s => s.Projects ?? Enumerable.Empty()) + .DistinctBy(p => p.Id) + .Select(p => Mapper.Map(p)) + .ToDictionary(p => p.Id, p => p); + + dbContext.AttachRange(projects.Values); + + foreach (var s in secrets) { - var dbContext = GetDatabaseContext(scope); - var entities = new List(); - var projects = secrets - .SelectMany(s => s.Projects ?? Enumerable.Empty()) - .DistinctBy(p => p.Id) - .Select(p => Mapper.Map(p)) - .ToDictionary(p => p.Id, p => p); + var entity = Mapper.Map(s); - dbContext.AttachRange(projects); - - foreach (var s in secrets) + if (s.Projects?.Count > 0) { - var entity = Mapper.Map(s); - - if (s.Projects?.Count > 0) - { - entity.Projects = s.Projects.Select(p => projects[p.Id]).ToList(); - } - - entities.Add(entity); + entity.Projects = s.Projects.Select(p => projects[p.Id]).ToList(); } - await GetDbSet(dbContext).AddRangeAsync(entities); - await dbContext.SaveChangesAsync(); - } - return secrets; - } - catch (Exception e) - { - Console.WriteLine(e); - } + entities.Add(entity); + } + await GetDbSet(dbContext).AddRangeAsync(entities); + await dbContext.SaveChangesAsync(); + } return secrets; } }