public void DTO2DB_Defects(DTO.DefectsDTO dto, ref Defects db) { if (dto.defectsImageDTOs != null) { foreach (var item in db.DefectsImage.ToArray()) { if (!dto.defectsImageDTOs.Select(s => s.DefectImageID).Contains(item.DefectImageID)) { db.DefectsImage.Remove(item); } } foreach (var item in dto.defectsImageDTOs) { DefectsImage dbDetail = new DefectsImage(); if (item.DefectImageID < 0) { db.DefectsImage.Add(dbDetail); } else { dbDetail = db.DefectsImage.Where(o => o.DefectImageID == item.DefectImageID).FirstOrDefault(); } if (dbDetail != null) { AutoMapper.Mapper.Map <DTO.DefectsImageDTO, DefectsImage>(item, dbDetail); } } } AutoMapper.Mapper.Map <DTO.DefectsDTO, Defects>(dto, db); db.DefectA = dto.DefectA; db.DefectB = dto.DefectB; db.DefectC = dto.DefectC; }
public override bool UpdateData(int userId, int id, ref object dtoItem, out Notification notification) { DTO.DefectsDTO defetcsDTO = ((JObject)dtoItem).ToObject <DTO.DefectsDTO>(); notification = new Notification { Type = NotificationType.Success }; _TempFolder = FrameworkSetting.Setting.AbsoluteUserTempFolder + userId.ToString() + @"\"; try { using (var context = CreateContext()) { Defects defects = null; if (id == 0) { defects = new Defects(); context.Defects.Add(defects); } if (id > 0) { defects = context.Defects.FirstOrDefault(o => o.DefectID == id); } if (defects == null) { notification = new Notification { Type = NotificationType.Error, Message = "Can't Find Data" }; return(false); } else { //process tdfile foreach (DTO.DefectsImageDTO image in defetcsDTO.defectsImageDTOs.Where(o => o.ScanHasChange)) { image.FileUD = fwFactory.CreateFilePointer(this._TempFolder, image.ScanNewFile, image.FileUD); } this.converter.DTO2DB_Defects(defetcsDTO, ref defects); //remove tdfile foreach (DefectsImage dbDFImage in context.DefectsImage.Local.Where(o => o.Defects == null).ToList()) { if (!string.IsNullOrEmpty(dbDFImage.FileUD)) { fwFactory.RemoveImageFile(dbDFImage.FileUD); } } //remove orphan context.DefectsImage.Local.Where(o => o.Defects == null).ToList().ForEach(o => context.DefectsImage.Remove(o)); context.SaveChanges(); dtoItem = this.GetData(defects.DefectID, out notification); return(true); } } } catch (Exception ex) { notification = new Notification { Type = NotificationType.Error, Message = ex.Message }; return(false); } }