title: EF 4: How to properly update object in DbContext using MVC with repository pattern
body: <p>I'm trying to implement an AuditLog using the DBContext's ChangeTracker object, I ran into an issue where the <code>DbEntityEntry.OriginalValues</code> were getting wiped out and replaced with the <code>DbEntityEntry.CurrentValues</code>.  It was brought to my attention that the problem was <em>how</em> I was updating the object that was being tracked in the DbContext (original post: <a href="https://stackoverflow.com/questions/9588352/entity-framework-dbcontext-savechanges-originalvalue-incorrect">Entity Framework DbContext SaveChanges() OriginalValue Incorrect</a>).</p>
  
  <p>So now I need some help on the proper way to update a persisted object using the repository pattern in MVC 3 with Entity Framework 4.  This example code is adapted from the SportsStore application in the Pro Asp.NET MVC 3 Framework book put out by Apress.</p>
  
  <p>Here is my 'Edit' post action in the AdminController:</p>
  
  <pre><code>[HttpPost]
  public ActionResult Edit(Product product)
  {
      if (ModelState.IsValid)
      {
          // Here is the line of code of interest...
          repository.SaveProduct(product, User.Identity.Name);
  
          TempData["message"] = string.Format("{0} has been saved", product.Name);
          return RedirectToAction("Index");
      }
      else
      {
          // there is something wrong with the data values
          return View(product);
      }
  }
  </code></pre>
  
  <p>This calls into concrete class EFProductRepository (which is implementing the IProductRepository interface and injected via Ninject).  Here is the <code>SaveProduct</code> method in the concrete repository class:</p>
  
  <pre><code>public void SaveProduct(Product product, string userID)
  {
      if (product.ProductID == 0)
      {
          context.Products.Add(product);
      }
      else
      {
          context.Entry(product).State = EntityState.Modified;
      }
      context.SaveChanges(userID);
  }
  </code></pre>
  
  <p>The problem (as was brought to my attention in my previous SO post), is that when <code>context.Entry(product).State = EntityState.Modified;</code> is called it somehow messes up the ChangeTracker's ability to report on the changes.  So in my overloaded DBContext.SaveChanges(string userID) method, I am not seeing accurate values in the <code>ChangeTracker.Entries().Where(p =&gt; p.State == System.Data.EntityState.Modified).OriginalValues</code> object.</p>
  
  <p>If I update my EFProductRepository.SaveProduct method to this it works:</p>
  
  <pre><code>public void SaveProduct(Product product, string userID)
  {
      if (product.ProductID == 0)
      {
          context.Products.Add(product);
      }
      else
      {
          Product prodToUpdate = context.Products
            .Where(p =&gt; p.ProductID == product.ProductID).FirstOrDefault();
  
          if (prodToUpdate != null)
          {
              // Just updating one property to demonstrate....
              prodToUpdate.Name = product.Name;
          }
      }
      context.SaveChanges(userID);
  }
  </code></pre>
  
  <p>I would like to know the proper way to update the Product object and persist it in this scenario in such a way that the ChangeTracker accurately tracks my changes to the POCO class in the repository.  Am I supposed to do the latter example (except of course copying over all fields that may have been updated), or should I be taking a different approach?</p>
  
  <p>In this example the "Product" class is very simple and only has string properties and decimal properties.  In our real application we will have "complex" types and the POCO classes will reference other objects (i.e. Person that has a list of addresses).  I know I may also need to do something special to track the changes in this case.  Perhaps knowledge of this will change some advice that I receive here.</p>