public async Task <ImportResult> ImportProject(Project existingProject, Project importedProject, bool preserveIds) { List <File> lockedFiles = new List <File>(); List <AsyncLockResult> fileLocks = new List <AsyncLockResult>(); try { foreach (var directory in importedProject.Directories.Where(x => !x.ParentId.HasValue)) { var existingDir = existingProject.Directories.FirstOrDefault(x => x.Name.Equals(directory.Name)); if (existingDir == null) { if (preserveIds && directory.Id != Guid.Empty) { existingDir = new Directory(directory.Name, id: directory.Id); } else { existingDir = new Directory(directory.Name); } _db.Entry(existingDir).State = EntityState.Added; existingDir.ProjectId = existingProject.Id; existingProject.Directories.Add(existingDir); } lockedFiles.AddRange((await this.ImportDirectoryInternal(existingDir, directory, preserveIds, fileLocks)).LockedFiles); } } finally { foreach (var lockResult in fileLocks) { lockResult.Dispose(); } } return(new ImportResult { LockedFiles = lockedFiles }); }