// DTO 2 DB public void DTO2DB_SampleOrder(DTO.SampleOrderDTO dtoItem, ref SampleOrder dbItem) { AutoMapper.Mapper.Map <DTO.SampleOrderDTO, SampleOrder>(dtoItem, dbItem); if (!string.IsNullOrEmpty(dtoItem.Deadline)) { if (DateTime.TryParse(dtoItem.Deadline, nl, System.Globalization.DateTimeStyles.None, out tmpDate)) { dbItem.Deadline = tmpDate; } } if (!string.IsNullOrEmpty(dtoItem.HardDeadline)) { if (DateTime.TryParse(dtoItem.HardDeadline, nl, System.Globalization.DateTimeStyles.None, out tmpDate)) { dbItem.HardDeadline = tmpDate; } } if (dtoItem.SampleOrderID <= 0) { dbItem.ClientID = dtoItem.ClientID; dbItem.Season = dtoItem.Season; } // monitor foreach (SampleMonitor dbMonitor in dbItem.SampleMonitor.ToArray()) { if (!dtoItem.SampleMonitorDTOs.Select(o => o.SampleMonitorID).Contains(dbMonitor.SampleMonitorID)) { dbItem.SampleMonitor.Remove(dbMonitor); } } foreach (DTO.SampleMonitorDTO dtoMonitor in dtoItem.SampleMonitorDTOs) { SampleMonitor dbMonitor; if (dtoMonitor.SampleMonitorID <= 0) { dbMonitor = new SampleMonitor(); dbItem.SampleMonitor.Add(dbMonitor); } else { dbMonitor = dbItem.SampleMonitor.FirstOrDefault(o => o.SampleMonitorID == dtoMonitor.SampleMonitorID); } if (dbMonitor != null) { AutoMapper.Mapper.Map <DTO.SampleMonitorDTO, SampleMonitor>(dtoMonitor, dbMonitor); } } }
public bool UpdateOrderData(int userId, int id, ref object dtoItem, out Library.DTO.Notification notification) { DTO.SampleOrderDTO dtoOrder = ((Newtonsoft.Json.Linq.JObject)dtoItem).ToObject <DTO.SampleOrderDTO>(); notification = new Library.DTO.Notification() { Type = Library.DTO.NotificationType.Success }; try { using (Sample3MngEntities context = CreateContext()) { SampleOrder dbItem = null; if (id == 0) { dbItem = new SampleOrder(); context.SampleOrder.Add(dbItem); dbItem.CreatedBy = userId; dbItem.CreatedDate = DateTime.Now; dbItem.SampleOrderStatusID = 1; // pending status } else { dbItem = context.SampleOrder.FirstOrDefault(o => o.SampleOrderID == id); } if (dbItem == null) { notification.Message = "Sample Order not found!"; return(false); } else { dbItem.UpdatedBy = userId; dbItem.UpdatedDate = DateTime.Now; converter.DTO2DB_SampleOrder(dtoOrder, ref dbItem); context.SaveChanges(); if (id <= 0) { // generate order number using (DbContextTransaction scope = context.Database.BeginTransaction()) { context.Database.ExecuteSqlCommand("SELECT * FROM SampleOrder WITH (TABLOCKX, HOLDLOCK)"); try { dbItem.SampleOrderUD = dbItem.SampleOrderID.ToString("D8"); context.SaveChanges(); } catch (Exception ex) { throw ex; } finally { scope.Commit(); } } } } dtoItem = GetOrderData(dbItem.SampleOrderID, 0, string.Empty, out notification).Data; return(true); } } catch (Exception ex) { notification = new Library.DTO.Notification() { Message = ex.Message, Type = Library.DTO.NotificationType.Error }; return(false); } }