public bool Delete(int id, out Notification notification) { notification = new Library.DTO.Notification() { Type = Library.DTO.NotificationType.Success }; try { using (DraftPackingListMngEntities context = CreateContext()) { DraftPackingList dbItem = context.DraftPackingList.FirstOrDefault(o => o.DraftPackingListID == id); if (dbItem == null) { notification.Message = "Invoice not found!"; return(false); } else { List <DraftPackingListDetail> need_delete_details = new List <DraftPackingListDetail>(); foreach (DraftPackingListDetail dbDetail in dbItem.DraftPackingListDetail) { need_delete_details.Add(dbDetail); } foreach (var item in need_delete_details) { context.DraftPackingListDetail.Remove(item); } context.DraftPackingList.Remove(dbItem); context.SaveChanges(); return(true); } } } catch (Exception ex) { notification.Type = Library.DTO.NotificationType.Error; notification.Message = ex.Message; notification.DetailMessage.Add(ex.Message); if (ex.GetBaseException() != null) { notification.DetailMessage.Add(ex.GetBaseException().Message); } return(false); } }
public override bool UpdateData(int userId, int id, ref object dtoItem, out Notification notification) { notification = new Notification { Type = NotificationType.Success }; DTO.DraftPackingListDTO dtoDraftPackingList = ((Newtonsoft.Json.Linq.JObject)dtoItem).ToObject <DTO.DraftPackingListDTO>(); try { using (DraftPackingListMngEntities context = CreateContext()) { DraftPackingList dbItem = null; if (id == 0) { dbItem = new DraftPackingList(); context.DraftPackingList.Add(dbItem); } else { dbItem = context.DraftPackingList.FirstOrDefault(o => o.DraftPackingListID == id); } if (dbItem == null) { notification.Message = "Invoice not found!"; return(false); } else { converter.DTO2DB_DraftPackingList(dtoDraftPackingList, ref dbItem); if (id == 0) { dbItem.CreatedBy = userId; dbItem.CreatedDate = DateTime.Now; } else { dbItem.UpdatedBy = userId; dbItem.UpdatedDate = DateTime.Now; } context.DraftPackingListDetail.Local.Where(o => o.DraftPackingList == null).ToList().ForEach(o => context.DraftPackingListDetail.Remove(o)); context.SaveChanges(); Hashtable filter = new Hashtable(); filter["id"] = dbItem.DraftPackingListID; dtoItem = GetData(filter, out notification); return(true); } } } catch (Exception ex) { notification.Type = Library.DTO.NotificationType.Error; notification.Message = ex.Message; if (ex.InnerException != null && !string.IsNullOrEmpty(ex.InnerException.Message)) { notification.DetailMessage.Add(ex.InnerException.Message); } return(false); } }