// 3. public void DTO2DB_UpdateReceivingNote(DTO.ReceivingNote dtoItem, ref ReceivingNote dbItem) { if (dtoItem.ReceivingNoteDetails != null) { foreach (var item in dbItem.ReceivingNoteDetail) { if (!dtoItem.ReceivingNoteDetails.Select(s => s.ReceivingNoteDetailID).Contains(item.ReceivingNoteDetailID)) { dbItem.ReceivingNoteDetail.Remove(item); } } foreach (var item in dtoItem.ReceivingNoteDetails) { ReceivingNoteDetail receivingNoteDetail; if (item.ReceivingNoteDetailID < 0) { receivingNoteDetail = new ReceivingNoteDetail(); dbItem.ReceivingNoteDetail.Add(receivingNoteDetail); } else { receivingNoteDetail = dbItem.ReceivingNoteDetail.FirstOrDefault(s => s.ReceivingNoteDetailID == item.ReceivingNoteDetailID); } if (receivingNoteDetail != null) { AutoMapper.Mapper.Map <DTO.ReceivingNoteDetail, ReceivingNoteDetail>(item, receivingNoteDetail); } } } AutoMapper.Mapper.Map <DTO.ReceivingNote, ReceivingNote>(dtoItem, dbItem); }
public override bool UpdateData(int userId, int id, ref object dtoItem, out Library.DTO.Notification notification) { notification = new Library.DTO.Notification(); notification.Type = Library.DTO.NotificationType.Success; List <DTO.ReceivingNote> receivingNotes = ((Newtonsoft.Json.Linq.JArray)dtoItem).ToObject <List <DTO.ReceivingNote> >(); Framework.DAL.DataFactory frameworkFactory = new Framework.DAL.DataFactory(); bool result = true; using (var context = CreatedContext()) { using (var trans = context.Database.BeginTransaction()) { try { foreach (var item in receivingNotes) { ReceivingNote dbItem; if (item.ReceivingNoteID < 0) { dbItem = new ReceivingNote(); context.ReceivingNote.Add(dbItem); } else { dbItem = context.ReceivingNote.FirstOrDefault(o => o.ReceivingNoteID == item.ReceivingNoteID); if (dbItem == null) { continue; } } converter.DTO2DB_UpdateReceivingNote(item, ref dbItem); dbItem.WorkCenterID = context.SupportMng_OPSequenceDetail_View.FirstOrDefault(o => o.WorkCenterID == item.OPSequenceDetailID).WorkCenterID; dbItem.ReceivingNoteDate = DateTime.Now; dbItem.CompanyID = frameworkFactory.GetCompanyID(userId); dbItem.UpdatedBy = userId; dbItem.UpdatedDate = DateTime.Now; context.ReceivingNoteDetail.Local.Where(o => o.ReceivingNote == null).ToList().ForEach(o => context.ReceivingNoteDetail.Remove(o)); context.SaveChanges(); context.ReceivingNoteMng_function_GenerateReceivingNoteUD(dbItem.ReceivingNoteID, dbItem.CompanyID, dbItem.ReceivingNoteDate.Value.Year, dbItem.ReceivingNoteDate.Value.Month); } trans.Commit(); } catch (Exception ex) { trans.Rollback(); notification.Type = Library.DTO.NotificationType.Error; notification.Message = ex.Message; result = false; } } } return(result); }