public void DTO2BD_Forwarder(DTO.ForwarderMng.Forwarder dtoItem, ref Forwarder dbItem, string tempFolder) { if (dtoItem.ForwarderImages != null) { foreach (var item in dbItem.ForwarderImage.ToArray()) { if (!dtoItem.ForwarderImages.Select(s => s.ForwarderImageID).Contains(item.ForwarderImageID)) { dbItem.ForwarderImage.Remove(item); } } foreach (var dto in dtoItem.ForwarderImages) { ForwarderImage item; if (dto.ForwarderImageID < 0) { item = new ForwarderImage(); dbItem.ForwarderImage.Add(item); } else { item = dbItem.ForwarderImage.FirstOrDefault(s => s.ForwarderImageID == dto.ForwarderImageID); } if (item != null) { AutoMapper.Mapper.Map <DTO.ForwarderMng.ForwarderImage, ForwarderImage>(dto, item); if (dto.File_HasChange.HasValue && dto.File_HasChange.Value) { Module.Framework.DAL.DataFactory fwFactory = new Module.Framework.DAL.DataFactory(); item.FileUD = fwFactory.CreateNoneImageFilePointer(tempFolder, dto.File_NewFile, dto.FileUD); } } } } if (dtoItem.ForwarderPICs != null) { foreach (var item1 in dbItem.ForwarderPIC.ToArray()) { if (!dtoItem.ForwarderPICs.Select(s => s.ForwarderPICID).Contains(item1.ForwarderPICID)) { dbItem.ForwarderPIC.Remove(item1); } } foreach (var dto1 in dtoItem.ForwarderPICs) { ForwarderPIC item1; if (dto1.ForwarderPICID < 0) { item1 = new ForwarderPIC(); dbItem.ForwarderPIC.Add(item1); } else { item1 = dbItem.ForwarderPIC.FirstOrDefault(s => s.ForwarderPICID == dto1.ForwarderPICID); } if (item1 != null) { AutoMapper.Mapper.Map <DTO.ForwarderMng.ForwarderPIC, ForwarderPIC>(dto1, item1); } } } AutoMapper.Mapper.Map <DTO.ForwarderMng.Forwarder, Forwarder>(dtoItem, dbItem); }
public override bool UpdateData(int id, ref DTO.ForwarderMng.Forwarder dtoItem, out Library.DTO.Notification notification) { notification = new Library.DTO.Notification() { Type = Library.DTO.NotificationType.Success }; int number; string indexName; try { using (ForwarderMngEntities context = CreateContext()) { Forwarder dbItem = null; if (id == 0) { dbItem = new Forwarder(); context.Forwarder.Add(dbItem); } else { dbItem = context.Forwarder.FirstOrDefault(o => o.ForwarderID == id); } if (dbItem == null) { notification.Message = "Forwarder not found!"; return(false); } else { converter.DTO2BD_Forwarder(dtoItem, ref dbItem, this._TempFolder); context.ForwarderImage.Local.Where(o => o.Forwarder == null).ToList().ForEach(o => context.ForwarderImage.Remove(o)); context.ForwarderPIC.Local.Where(o => o.Forwarder == null).ToList().ForEach(o => context.ForwarderPIC.Remove(o)); context.SaveChanges(); dtoItem = GetData(dbItem.ForwarderID, out notification).Data; return(true); } } } catch (System.Data.DataException dEx) { notification.Type = Library.DTO.NotificationType.Error; Library.ErrorHelper.DataExceptionParser(dEx, out number, out indexName); if (number == 2601 && !string.IsNullOrEmpty(indexName)) { if (indexName == "ForwarderUDUnique") { notification.Message = "The Forwarder Code is already exists"; } } else { notification.Message = dEx.Message; } return(false); } catch (Exception ex) { notification.Type = Library.DTO.NotificationType.Error; notification.Message = ex.Message; return(false); } }