public async Task CreateComponentAsync(int productVersionId, ComponentEditModel component) { using (var scope = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled)) { var exportedTypes = await exportedTypeRepository.GetExportedTypesAsync(productVersionId); var existsComponent = await componentRepository.GetComponentAsync(component.TagName); var productVersion = productVersionRepository.QueryByKey(productVersionId); var exportedTypeIds = component.ExportedTypeIds.Select(x => Convert.ToInt32(x)).ToArray(); if (existsComponent == null) { var newComponent = new Repository.Model.Component() { Description = component.Description, ExportedTypes = exportedTypes.Where(x => exportedTypeIds.Contains(x.Id)).ToList(), Name = component.Name, TagName = component.TagName }; foreach (var exportedType in newComponent.ExportedTypes) { exportedType.Component = newComponent; } newComponent.Versions.Add(new ComponentVersion() { Component = newComponent, ProductVersion = productVersion }); await componentRepository.CreateAsync(newComponent); } else if (existsComponent.Versions.Any(x => x.ProductVersionId == productVersionId)) { throw new OperationException("该组件在该版本已存在"); } else { existsComponent.ExportedTypes = exportedTypes.Where(x => exportedTypeIds.Contains(x.Id)).ToList(); foreach (var exportedType in existsComponent.ExportedTypes) { exportedType.Component = existsComponent; } existsComponent.Name = component.Name; existsComponent.Versions.Add(new ComponentVersion() { Component = existsComponent, ProductVersion = productVersion }); await productVersionRepository.SaveChangesAsync(); } scope.Complete(); } }