title: Context does not update
body: <p>I've been reading and following the book "Pro ASP.Net MVC3 Framework Third Edition" by Freeman A. and Sanderson S.</p>
  
  <p>I've reached the part where they tell me how to update entities. Everything works fine, except the update.</p>
  
  <p>I've got a page to edit items in a store. Pretty basic. When I change values in the page, and submit them they reach the implementation of my repository they way they should, with the new values. </p>
  
  <p>Now, the book shows me the following method in my repository (SaveProduct):</p>
  
  <pre><code>namespace SportsStore.Domain.Concrete
  {
      public class EFProductRepository : IProductRepository
      {
           private EFDbContext context = new EFDbContext();
      public IQueryable&lt;Product&gt; Products
      {
          get { return context.Products; }
      }
      public void SaveProduct(Product product)
      {
          if (product.ProductID == 0)
          {
              context.Products.Add(product);
          }
          context.SaveChanges();
      }
  }
  }
  </code></pre>
  
  <p>This method works, except it does not save my product. I'm not fully on top of the vagueness of MVC3, so I'm stuck. The product arrives as parameter in the SaveProduct method, with the proper values, and should be saved, yet the context is not updated.</p>
  
  <p>Is this something that got updated in MVC3? (Is the book a little outdated?) Because I could code it with if's to check all the values separately, but I can't help feeling that that cancels the power of MVC3/Entity Framework.</p>
  
  <p>Could anyone help me out? Thanks a lot! :) In the meanwhile I'll be debugging and looking for any clue's!</p>
  
  <p>Edit:</p>
  
  <p>When I catch the integer that SaveChanges() returns, it seems to be zero. So I guess the entity with the new values is not "updated" to the entity framework.</p>
  
  <p>Edit 2:</p>
  
  <p>I've found the problem (already). Seems I was too quick asking questions, for which I apologize. Here is what I did:</p>
  
  <p>I added the following code to the method SaveProduct:</p>
  
  <pre><code>    public void SaveProduct(Product product)
      {
          if (product.ProductID == 0)
          {
              context.Products.Add(product);
          }
          else
          {
              var productInDb = context.Products.FirstOrDefault(x =&gt; x.ProductID == product.ProductID);
              context.Entry(productInDb).CurrentValues.SetValues(product);
          }
          context.SaveChanges();
      }
  </code></pre>
  
  <p>I guess this is one proper way of doing so? Sorry for the useless question :)</p>