public override bool DeleteData(int id, out Library.DTO.Notification notification) { notification = new Library.DTO.Notification { Type = Library.DTO.NotificationType.Success }; try { using (FactoryStepNormEntities context = CreateContext()) { var dbItem = context.FactoryStepNorm.Where(o => o.FactoryStepNormID == id).FirstOrDefault(); foreach (var item in dbItem.FactoryStepNormDetail.ToArray()) { context.FactoryStepNormDetail.Remove(item); } context.FactoryStepNorm.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 Library.DTO.Notification notification) { notification = new Library.DTO.Notification() { Type = Library.DTO.NotificationType.Success }; DTO.FactoryStepNorm dtoFactoryStepNorm = ((Newtonsoft.Json.Linq.JObject)dtoItem).ToObject <DTO.FactoryStepNorm>(); try { using (FactoryStepNormEntities context = CreateContext()) { FactoryStepNorm dbItem = null; if (id == 0) { dbItem = new FactoryStepNorm(); context.FactoryStepNorm.Add(dbItem); } else { dbItem = context.FactoryStepNorm.Where(o => o.FactoryStepNormID == id).FirstOrDefault(); } if (dbItem == null) { notification.Message = "data not found!"; return(false); } else { //convert dto to db converter.DTO2DB_FactoryStepNorm(dtoFactoryStepNorm, ref dbItem); //remove orphan item context.FactoryStepNormDetail.Local.Where(o => o.FactoryStepNorm == null).ToList().ForEach(o => context.FactoryStepNormDetail.Remove(o)); //save data context.SaveChanges(); //get return data dtoItem = GetData(dbItem.FactoryStepNormID, out notification).Data; 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); } }