public void DTO2BD(DTO.AVFOutwardInvoice dtoItem, ref AVFOutwardInvoice dbItem) { dbItem.InvoiceDate = dtoItem.InvoiceDate.ConvertStringToDateTime(); AutoMapper.Mapper.Map <DTO.AVFOutwardInvoice, AVFOutwardInvoice>(dtoItem, dbItem); if (dtoItem.AVFOutwardInvoiceDetails != null) { //check for child rows deleted foreach (AVFOutwardInvoiceDetail dbDetail in dbItem.AVFOutwardInvoiceDetail.ToArray()) { if (!dtoItem.AVFOutwardInvoiceDetails.Select(o => o.AVFOutwardInvoiceDetailID).Contains(dbDetail.AVFOutwardInvoiceDetailID)) { dbItem.AVFOutwardInvoiceDetail.Remove(dbDetail); } } //map child row foreach (DTO.AVFOutwardInvoiceDetail dtoDetail in dtoItem.AVFOutwardInvoiceDetails) { AVFOutwardInvoiceDetail dbDetail; if (dtoDetail.AVFOutwardInvoiceDetailID <= 0) { dbDetail = new AVFOutwardInvoiceDetail(); dbItem.AVFOutwardInvoiceDetail.Add(dbDetail); } else { dbDetail = dbItem.AVFOutwardInvoiceDetail.FirstOrDefault(o => o.AVFOutwardInvoiceDetailID == dtoDetail.AVFOutwardInvoiceDetailID); } if (dbDetail != null) { AutoMapper.Mapper.Map <DTO.AVFOutwardInvoiceDetail, AVFOutwardInvoiceDetail>(dtoDetail, dbDetail); } } AutoMapper.Mapper.Map <DTO.AVFOutwardInvoice, AVFOutwardInvoice>(dtoItem, dbItem); } }
public override bool UpdateData(int userId, int id, ref object dtoItem, out Library.DTO.Notification notification) { DTO.AVFOutwardInvoice dtoAVFOutwardInvoice = ((Newtonsoft.Json.Linq.JObject)dtoItem).ToObject <DTO.AVFOutwardInvoice>(); notification = new Library.DTO.Notification() { Type = Library.DTO.NotificationType.Success }; try { using (AVFOutwardInvoiceMngEntities context = CreateContext()) { AVFOutwardInvoice dbItem = null; if (id == 0) { dbItem = new AVFOutwardInvoice(); context.AVFOutwardInvoice.Add(dbItem); } else { dbItem = context.AVFOutwardInvoice.FirstOrDefault(o => o.AVFOutwardInvoiceID == id); } if (dbItem == null) { notification.Message = "Invoice not found!"; return(false); } else { // check concurrency if (dbItem.ConcurrencyFlag != null && !dbItem.ConcurrencyFlag.SequenceEqual(Convert.FromBase64String(dtoAVFOutwardInvoice.ConcurrencyFlag_String))) { throw new Exception(Library.Helper.TEXT_CONCURRENCY_CONFLICT); } //convert dto to db converter.DTO2BD(dtoAVFOutwardInvoice, ref dbItem); //remove orphan item context.AVFOutwardInvoiceDetail.Local.Where(o => o.AVFOutwardInvoice == null).ToList().ForEach(o => context.AVFOutwardInvoiceDetail.Remove(o)); // processing file //if (dtoAVFOutwardInvoice.PDFFileScan_HasChange) //{ // dbItem.PDFFileScan = fwFactory.CreateNoneImageFilePointer(this._tempFolder, dtoAVFOutwardInvoice.PDFFileScan_NewFile, dtoAVFOutwardInvoice.PDFFileScan); //} dbItem.UpdatedDate = DateTime.Now; dbItem.UpdatedBy = userId; context.SaveChanges(); dtoItem = GetData(dbItem.AVFOutwardInvoiceID, out notification).Data; return(true); } } } catch (Exception ex) { notification.Type = Library.DTO.NotificationType.Error; notification.Message = ex.Message; return(false); } }