public async Task <int> CreateAsync(FinishingPrintingCostCalculationModel model) { int created = 0; finishingPrintingCostCalculationLogic.Create(model); created += await DbContext.SaveChangesAsync(); var chemicalModels = model.Machines.ToList().SelectMany(entity => { var result = entity.Chemicals.ToList().Select(chemical => { chemical.CostCalculationId = model.Id; return(chemical); }); return(result); }); ChemicalDBSet.UpdateRange(chemicalModels); created += await DbContext.SaveChangesAsync(); return(created); }
public async Task <int> CreateAsync(FinishingPrintingCostCalculationModel model) { int created = 0; using (var transaction = DbContext.Database.BeginTransaction()) { try { finishingPrintingCostCalculationLogic.Create(model); created += await DbContext.SaveChangesAsync(); foreach (var machine in model.Machines) { foreach (var chemical in machine.Chemicals) { chemical.CostCalculationId = model.Id; } } if (!string.IsNullOrWhiteSpace(model.ImageFile)) { model.ImagePath = await AzureImageFacade.UploadImage(model.GetType().Name, model.Id, model.CreatedUtc, model.ImageFile); } created += await DbContext.SaveChangesAsync(); transaction.Commit(); return(created); } catch (Exception ex) { transaction.Rollback(); throw ex; } } }