public void DeleteProductVariant(ProductVariant productVariant) { bool saveFailed = true; context.ProductVariants.Remove(productVariant); while (saveFailed) { saveFailed = false; try { context.SaveChanges(); } catch (DbUpdateConcurrencyException ex) { saveFailed = true; // Reload with StoreWins ex.Entries.Single().Reload(); } catch (Exception ex) { throw ex; } } }
public void SaveProductVariant(ProductVariant productVariant) { bool saveFailed = true; if (productVariant.VariantID == 0) { context.ProductVariants.Add(productVariant); } else { // If "productVariant" is not from tracked entity (var pv = repo.ProductVariant.First() ) // But from new entity obj ... ViewModel return at POST request // Then do //context.ProductVariants.Attach(productVariant) //productVariant.Name = "xyz"; // context.Savechanges() context.Entry(productVariant).State = EntityState.Modified; } // SaveProduct(productVariant.Product); // another way to edit //var found = context.Products.Find(product.ProductID); //if (found == null) // context.Products.Add(product); //else //{ // var entry = context.Entry(found); // entry.OriginalValues.SetValues(found); // entry.CurrentValues.SetValues(product); //} // Optimistic Concurrency implementation while (saveFailed) { saveFailed = false; try { // or int numSaved = context.SaveChanges(); context.SaveChanges(); } catch (DbUpdateConcurrencyException ex) { saveFailed = true; // Reload with StoreWins ex.Entries.Single().Reload(); } catch (Exception ex) { throw ex; } } }
public ActionResult TestEdit(ProductVariant pv) { // productRepo.SaveProduct(p); productRepo.SaveProductVariant(pv); return View("TaskCompleted"); }