public void UpdateProduct(Product changedProduct, Product originalProduct = null) { //Console.WriteLine("UpdateProduct: " + JsonConvert.SerializeObject(changedProduct)); //context.Products.Update(changedProduct); //context.SaveChanges(); if (originalProduct == null) { originalProduct = context.Products.Find(changedProduct.Id); } else { context.Products.Attach(originalProduct); } originalProduct.Name = changedProduct.Name; originalProduct.Category = changedProduct.Category; originalProduct.Price = changedProduct.Price; EntityEntry entry = context.Entry(originalProduct); Console.WriteLine($"Entity State: {entry.State}"); foreach (string p_name in new string[] { "Name", "Category", "Price" }) { Console.WriteLine($"{p_name} - Old: {entry.OriginalValues[p_name]}, New: {entry.CurrentValues[p_name]}"); } context.SaveChanges(); }
public void UpdateProduct(Product changedProduct, Product originalProduct = null) { if (originalProduct == null) { originalProduct = context.Products.Find(changedProduct.Id); } else { context.Products.Attach(originalProduct); } originalProduct.Name = changedProduct.Name; originalProduct.Category = changedProduct.Category; originalProduct.Price = changedProduct.Price; originalProduct.Supplier.Name = changedProduct.Supplier.Name; originalProduct.Supplier.City = changedProduct.Supplier.City; originalProduct.Supplier.State = changedProduct.Supplier.State; EntityEntry entry = context.Entry(originalProduct); Console.WriteLine($"Entity State: {entry.State}"); foreach (string p_name in new string[] { "Name", "Category", "Price" }) { Console.WriteLine($"{p_name} - Old: " + $"{entry.OriginalValues[p_name]}, " + $"New: {entry.CurrentValues[p_name]}"); } context.SaveChanges(); }