public bool Update(int userId, int id, ref object dto, out Notification notification) { DTO.CushionTestReportDTO dtoItem = ((Newtonsoft.Json.Linq.JObject)dto).ToObject <DTO.CushionTestReportDTO>(); notification = new Notification() { Type = NotificationType.Success }; try { using (CushionTestingMngEntities context = CreateContext()) { CushionTestReport dbItem = null; using (DbContextTransaction scope = context.Database.BeginTransaction()) { context.Database.ExecuteSqlCommand("SELECT * FROM CushionTestReport WITH (TABLOCKX, HOLDLOCK)"); try { if (id > 0) { dbItem = context.CushionTestReport.FirstOrDefault(s => s.CushionTestReportID == id); if (dbItem == null) { notification = new Notification() { Type = NotificationType.Error, Message = "Can not find data" }; return(false); } } else { dbItem = new CushionTestReport(); context.CushionTestReport.Add(dbItem); } converter.DTO2DB_CushionTestReport(dtoItem, ref dbItem, FrameworkSetting.Setting.AbsoluteUserTempFolder + userId.ToString() + @"\", userId); dbItem.UpdatedBy = userId; dbItem.UpdatedDate = DateTime.Now; context.CushionTestReportFile.Local.Where(o => o.CushionTestReport == null).ToList().ForEach(s => context.CushionTestReportFile.Remove(s)); context.CushionTestReportUsingCushionStandard.Local.Where(o => o.CushionTestReport == null).ToList().ForEach(o => context.CushionTestReportUsingCushionStandard.Remove(o)); context.SaveChanges(); //Create code of id dbItem.CushionTestReportUD = "C" + dbItem.CushionTestReportID.ToString("D9"); context.SaveChanges(); } catch (Exception ex) { throw ex; } finally { scope.Commit(); } } dto = GetEdit(dbItem.CushionTestReportID, out notification).Data; } return(true); } catch (Exception ex) { notification = new Notification() { Type = NotificationType.Error, Message = ex.Message }; return(false); } }
public void DTO2DB_CushionTestReport(DTO.CushionTestReportDTO dtoItem, ref CushionTestReport dbItem, string TmpFile, int userId) { foreach (CushionTestReportFile dbFile in dbItem.CushionTestReportFile.ToArray()) { if (!dtoItem.CushionTestReportFileDTOs.Select(o => o.CushionTestReportFileID).Contains(dbFile.CushionTestReportFileID)) { if (!string.IsNullOrEmpty(dbFile.FileUD)) { // remove file fwFactory.RemoveImageFile(dbFile.FileUD); } dbItem.CushionTestReportFile.Remove(dbFile); } } foreach (DTO.CushionTestReportFileDTO dtoFile in dtoItem.CushionTestReportFileDTOs) { CushionTestReportFile dbFile; if (dtoFile.CushionTestReportFileID <= 0) { dbFile = new CushionTestReportFile(); dbItem.CushionTestReportFile.Add(dbFile); } else { dbFile = dbItem.CushionTestReportFile.FirstOrDefault(o => o.CushionTestReportFileID == dtoFile.CushionTestReportFileID); } if (dbFile != null) { // change or add file if (dtoFile.ScanHasChange) { dtoFile.FileUD = fwFactory.CreateNoneImageFilePointer(TmpFile, dtoFile.ScanNewFile, dtoFile.FileUD, dtoFile.FriendlyName); } AutoMapper.Mapper.Map <DTO.CushionTestReportFileDTO, CushionTestReportFile>(dtoFile, dbFile); } } if (dtoItem.CushionTestReportFileDTOs != null) { foreach (CushionTestReportFile item in dbItem.CushionTestReportFile.ToList()) { if (!dtoItem.CushionTestReportFileDTOs.Select(s => s.CushionTestReportFileID).Contains(item.CushionTestReportFileID)) { dbItem.CushionTestReportFile.Remove(item); } } foreach (DTO.CushionTestReportFileDTO dto in dtoItem.CushionTestReportFileDTOs) { CushionTestReportFile item; if (dto.CushionTestReportFileID < 0) { item = new CushionTestReportFile(); dbItem.CushionTestReportFile.Add(item); } else { item = dbItem.CushionTestReportFile.FirstOrDefault(s => s.CushionTestReportFileID == dto.CushionTestReportFileID); } if (item != null) { AutoMapper.Mapper.Map <DTO.CushionTestReportFileDTO, CushionTestReportFile>(dto, item); } } } if (dtoItem.CushionTestStandardDTOs != null) { foreach (var item in dbItem.CushionTestReportUsingCushionStandard.ToArray()) { if (!dtoItem.CushionTestStandardDTOs.Select(o => o.CushionTestReportUsingCushionStandardID).Contains(item.CushionTestReportUsingCushionStandardID)) { dbItem.CushionTestReportUsingCushionStandard.Remove(item); } } foreach (var item in dtoItem.CushionTestStandardDTOs) { CushionTestReportUsingCushionStandard dbTestStandard = new CushionTestReportUsingCushionStandard(); if (item.CushionTestReportUsingCushionStandardID < 0) { dbItem.CushionTestReportUsingCushionStandard.Add(dbTestStandard); } else { dbTestStandard = dbItem.CushionTestReportUsingCushionStandard.Where(s => s.CushionTestReportUsingCushionStandardID == item.CushionTestReportUsingCushionStandardID).FirstOrDefault(); } if (dbTestStandard != null) { AutoMapper.Mapper.Map <DTO.CushionTestStandardDTO, CushionTestReportUsingCushionStandard>(item, dbTestStandard); } } } AutoMapper.Mapper.Map <DTO.CushionTestReportDTO, CushionTestReport>(dtoItem, dbItem); dbItem.TestDate = dtoItem.TestDate.ConvertStringToDateTime(); }