public bool Update(int userId, int id, ref object dto, out Notification notification) { DTO.OPSequenceDto dtoItem = ((Newtonsoft.Json.Linq.JObject)dto).ToObject <DTO.OPSequenceDto>(); notification = new Notification() { Type = NotificationType.Success }; try { using (OPSequenceEntities context = CreateContext()) { OPSequence dbItem; if (id > 0) { if (this.HasBOM(id)) { throw new Exception("There are some BOM which are using OP Sequence, so you can not change !!!"); } dbItem = context.OPSequence.FirstOrDefault(s => s.OPSequenceID == id); if (dbItem == null) { notification = new Notification() { Type = NotificationType.Error, Message = "Can not find data" }; return(false); } } else { dbItem = new OPSequence(); context.OPSequence.Add(dbItem); } converter.DTO2DB_Update(dtoItem, ref dbItem); dbItem.CompanyID = fwFactory.GetCompanyID(userId); dbItem.UpdatedBy = userId; dbItem.UpdatedDate = DateTime.Now; context.OPSequenceDetail.Local.Where(o => o.OPSequence == null).ToList().ForEach(o => context.OPSequenceDetail.Remove(o)); context.SaveChanges(); dto = Get(dbItem.OPSequenceID, out notification).Data; } return(true); } catch (Exception ex) { notification = new Notification() { Type = NotificationType.Error, Message = ex.Message }; return(false); } }
public void DTO2DB_Update(DTO.OPSequenceDto dtoItem, ref OPSequence dbItem) { if (dtoItem.OPSequenceDetails != null) { foreach (OPSequenceDetail item in dbItem.OPSequenceDetail.ToArray()) { if (!dtoItem.OPSequenceDetails.Select(s => s.OPSequenceDetailID).Contains(item.OPSequenceDetailID)) { dbItem.OPSequenceDetail.Remove(item); } } foreach (DTO.OPSequenceDetailDto dto in dtoItem.OPSequenceDetails) { OPSequenceDetail item; if (dto.OPSequenceDetailID < 0) { item = new OPSequenceDetail(); dbItem.OPSequenceDetail.Add(item); } else { item = dbItem.OPSequenceDetail.FirstOrDefault(s => s.OPSequenceDetailID == dto.OPSequenceDetailID); } if (item != null) { AutoMapper.Mapper.Map <DTO.OPSequenceDetailDto, OPSequenceDetail>(dto, item); } } } AutoMapper.Mapper.Map <DTO.OPSequenceDto, OPSequence>(dtoItem, dbItem); }