public async Task <bool> DeleteEmpyItems(InvoiceHeaderSet invoiceHeaderSet) { await Task.Run(() => { using (CasierContents context = new CasierContents()) { if (invoiceHeaderSet != null) { InvoiceHeaderSet CurrentInvoiceHeaderSet = context.InvoiceHeaderSet.Include("InvoiceHeaderDetailsSet.ItemSet").Include("ReservationSet").Where(x => x.InvoiceHeaderSetId == invoiceHeaderSet.InvoiceHeaderSetId).FirstOrDefault() ?? null; if (CurrentInvoiceHeaderSet != null) { if (CurrentInvoiceHeaderSet.InvoiceHeaderDetailsSet.Count != 0) { foreach (var item in CurrentInvoiceHeaderSet.InvoiceHeaderDetailsSet.ToList()) { if (item.Quantity <= 0) { CurrentInvoiceHeaderSet.InvoiceHeaderDetailsSet.ToList().ForEach(p => p.InvoiceHeaderSet.InvoiceHeaderDetailsSet.Remove(item)); context.Entry(item).State = EntityState.Deleted; } } } else if (CurrentInvoiceHeaderSet.InvoiceHeaderDetailsSet.Count == 0 && CurrentInvoiceHeaderSet.ReservationSet == null) { context.InvoiceHeaderSet.Remove(CurrentInvoiceHeaderSet); } context.SaveChanges(); } } } }); return(true); }
public JsonResult DeleteItem(ItemSet items) { if (items != null) { using (CasierContents dataContext = new CasierContents()) { dataContext.Configuration.ProxyCreationEnabled = false; dataContext.Configuration.LazyLoadingEnabled = false; dataContext.Configuration.ValidateOnSaveEnabled = false; //var category = items.CategorySet; //var fintTheCategory = dataContext.CategorySet.Where(x => x.CategoryId == category.CategoryId).FirstOrDefault(); //if (fintTheCategory != null) { var findItem = dataContext.ItemSet.Include("InvoiceHeaderDetailsSet").Where(x => x.ItemSetId == items.ItemSetId).FirstOrDefault(); dataContext.ItemSet.Attach(findItem); dataContext.Entry(findItem).Collection("InvoiceHeaderDetailsSet").Load(); // findItem.CategorySet.ItemSet.Remove(findItem); findItem.InvoiceHeaderDetailsSet.ToList().ForEach(I => dataContext.InvoiceHeaderDetailsSet.Remove(I)); dataContext.ItemSet.Remove(findItem); dataContext.SaveChanges(); dataContext.SaveChanges(); var results = new { Success = "True", Message = "Succesfull" }; return(Json(results, JsonRequestBehavior.AllowGet)); } var result = new { Success = "True", Message = "Succesfull" }; return(Json(result, JsonRequestBehavior.AllowGet)); } } else { var result = new { Success = "True", Message = "Error" }; return(Json(result, JsonRequestBehavior.AllowGet)); } }