public void DeleteProduct(Product product) { bool saveFailed = true; context.Products.Remove(product); 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 IQueryable<Product> Products //{ // get { return context.Products; } //} public void SaveProduct(Product product) { bool saveFailed = true; if (product.ProductID == 0) { context.Products.Add(product); } else { context.Entry(product).State = EntityState.Modified; } // 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; } } }