I came across the same issue using the final version of Apress Pro ASP.NET MVC3. Using the Visual Studio debugger I noticed that when the context.SaveChanges()
(SportsStore.Domain.Concrete.EFProductRepoistory) was executed the context was not changed to the changes we made inside the Edit view. Though the product defined in the constructor of SaveProduct()
So i guessed all we had to do is change the Context.Products.Product to the product inside the constructor like this:
else
{
context.Products.Find(product.ProductId) = product;
}
unfortunately Visual Studio gave me this error:
Error 1 The left-hand side of an assignment must be a variable, property or indexer
So to make it work I had to do this:
else
{
context.Products.Find(product.ProductID).Name = product.Name;
context.Products.Find(product.ProductID).Description = product.Description;
context.Products.Find(product.ProductID).Category = product.Category;
context.Products.Find(product.ProductID).Price = product.Price;
}
This does work. However I think this is far from ideal and not the best way to do this.
Is there a way to do this in a way where I just edit/update the whole Product object inside the context rather than edit every property one by one?
context.Entry(product).State = EntityState.Modified; //using system.data;
to the EFProductRepository / SaveProduct. Wouldn't work for me otherwise - and I followed the examples implicitly. – Euraeurasia