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