public void DTO2DB_ProductInfo(DTO.ProductInfo.ProductDTO dtoItem, ref SampleProduct dbItem, string TmpFile) { AutoMapper.Mapper.Map <DTO.ProductInfo.ProductDTO, SampleProduct>(dtoItem, dbItem); // remark image foreach (SampleReferenceImage dbImage in dbItem.SampleReferenceImage.ToArray()) { if (!dtoItem.ReferenceImageDTOs.Select(o => o.SampleReferenceImageID).Contains(dbImage.SampleReferenceImageID)) { // delete files if (!string.IsNullOrEmpty(dbImage.FileUD)) { fwFactory.RemoveImageFile(dbImage.FileUD); } dbItem.SampleReferenceImage.Remove(dbImage); } } foreach (DTO.ProductInfo.ReferenceImageDTO dtoImage in dtoItem.ReferenceImageDTOs) { SampleReferenceImage dbImage; if (dtoImage.SampleReferenceImageID <= 0) { dbImage = new SampleReferenceImage(); dbItem.SampleReferenceImage.Add(dbImage); } else { dbImage = dbItem.SampleReferenceImage.FirstOrDefault(o => o.SampleReferenceImageID == dtoImage.SampleReferenceImageID); } if (dbItem != null) { AutoMapper.Mapper.Map <DTO.ProductInfo.ReferenceImageDTO, SampleReferenceImage>(dtoImage, dbImage); if (!string.IsNullOrEmpty(dtoImage.BringInDate)) { if (DateTime.TryParse(dtoImage.BringInDate, nl, System.Globalization.DateTimeStyles.None, out tmpDate)) { dbImage.BringInDate = tmpDate; } } if (dtoImage.HasChanged && !string.IsNullOrEmpty(dtoImage.NewFile)) { dbImage.FileUD = fwFactory.CreateFilePointer(TmpFile, dtoImage.NewFile, dbImage.FileUD); } } } }
public bool UpdateProductInfoData(int userId, int id, ref object dtoItem, out Library.DTO.Notification notification) { DTO.ProductInfo.ProductDTO dtoProduct = ((Newtonsoft.Json.Linq.JObject)dtoItem).ToObject <DTO.ProductInfo.ProductDTO>(); notification = new Library.DTO.Notification() { Type = Library.DTO.NotificationType.Success }; try { using (Sample3MngEntities context = CreateContext()) { SampleProduct dbItem; if (id <= 0) { dbItem = new SampleProduct(); context.SampleProduct.Add(dbItem); } else { dbItem = context.SampleProduct.FirstOrDefault(o => o.SampleProductID == id); } if (dbItem == null) { notification.Message = "Sample Product not found!"; return(false); } else { converter.DTO2DB_ProductInfo(dtoProduct, ref dbItem, FrameworkSetting.Setting.AbsoluteUserTempFolder + userId.ToString() + @"\"); context.SaveChanges(); } dtoItem = GetProductInfoData(dbItem.SampleProductID, out notification).Data; return(true); } } catch (Exception ex) { notification = new Library.DTO.Notification() { Message = ex.Message, Type = Library.DTO.NotificationType.Error }; return(false); } }