public override bool DeleteData(int id, out Library.DTO.Notification notification) { notification = new Library.DTO.Notification() { Type = Library.DTO.NotificationType.Success }; try { using (DDCMngEntities context = CreateContext()) { DDC dbItem = context.DDC.FirstOrDefault(o => o.DDCID == id); if (dbItem == null) { notification.Message = "DDC not found!"; return(false); } else { context.DDC.Remove(dbItem); context.SaveChanges(); return(true); } } } catch (Exception ex) { notification.Type = Library.DTO.NotificationType.Error; notification.Message = ex.Message; return(false); } }
public override bool UpdateData(int id, ref DTO.DDCMng.DDC dtoItem, out Library.DTO.Notification notification) { notification = new Library.DTO.Notification() { Type = Library.DTO.NotificationType.Success }; try { using (DDCMngEntities context = CreateContext()) { DDC dbItem = null; if (id == 0) { dbItem = new DDC(); context.DDC.Add(dbItem); } else { dbItem = context.DDC.FirstOrDefault(o => o.DDCID == id); } if (dbItem == null) { notification.Message = "DDC not found!"; return(false); } else { converter.DTO2BD(dtoItem, ref dbItem); context.DDCDetail.Local.Where(o => o.DDC == null).ToList().ForEach(o => context.DDCDetail.Remove(o)); context.SaveChanges(); dtoItem = GetData(dbItem.DDCID, string.Empty, out notification).Data; return(true); } } } catch (Exception ex) { notification.Type = Library.DTO.NotificationType.Error; notification.Message = ex.Message; return(false); } }
public void DTO2BD(DTO.DDCMng.DDC dtoItem, ref DDC dbItem) { AutoMapper.Mapper.Map <DTO.DDCMng.DDC, DDC>(dtoItem, dbItem); // map load ddc detail if (dtoItem.Details != null) { // check for child rows deleted foreach (DDCDetail dbDetail in dbItem.DDCDetail.ToArray()) { if (!dtoItem.Details.Select(o => o.DDCDetailID).Contains(dbDetail.DDCDetailID)) { dbItem.DDCDetail.Remove(dbDetail); } } // map child rows foreach (DTO.DDCMng.DDCDetail dtoDetail in dtoItem.Details) { DDCDetail dbDetail; if (dtoDetail.DDCDetailID <= 0) { dbDetail = new DDCDetail(); dbItem.DDCDetail.Add(dbDetail); } else { dbDetail = dbItem.DDCDetail.FirstOrDefault(o => o.DDCDetailID == dtoDetail.DDCDetailID); } if (dbDetail != null) { AutoMapper.Mapper.Map <DTO.DDCMng.DDCDetail, DDCDetail>(dtoDetail, dbDetail); } } } }