title: MVC3 with EF 4.1 and EntityState.Modified on update
body: <p>I'm having problems understanding EntityState.Modified when it comes to updating an object with .NET MVC3.</p>
  
  <p>I have a model that stores an ImageFilePath and ImageContentType when an image is uploaded. Here is what the create action looks like.</p>
  
  <pre><code>    [HttpPost]
      public ActionResult Create(SneakPeekCollection collection, HttpPostedFileBase image)
      {
          try
          {
              if (image != null)
              {
                  var filepath = Path.Combine(HttpContext.Server.MapPath("../../Uploads"), Path.GetFileName(image.FileName));
                  image.SaveAs(filepath);
                  collection.ImageContentType = image.ContentType;
                  collection.ImageFilePath = "~/Uploads/" + image.FileName;
  
              }
              _db.SneakPeekCollections.Add(collection);
              _db.SaveChanges();
              return RedirectToAction("Index");
          }
          catch
          {
              return View();
          }
      }
  </code></pre>
  
  <p>The problem comes when trying to edit and subsequently update this object. This is my Edit action.</p>
  
  <pre><code>    [HttpPost]
      public ActionResult Edit(int id, SneakPeekCollection collection, HttpPostedFileBase image)
      {
          try
          {
              if (image != null)
              {
                  var filepath = Path.Combine(HttpContext.Server.MapPath("../../../Uploads"), Path.GetFileName(image.FileName));
                  image.SaveAs(filepath);
                  collection.ImageContentType = image.ContentType;
                  collection.ImageFilePath = "~/Uploads/" + image.FileName;
              }
              _db.Entry(collection).State = EntityState.Modified;
              _db.SaveChanges();
              return RedirectToAction("Index");
          }
          catch
          {
              return View();
          }
      }
  </code></pre>
  
  <p>I believe the problem comes from the fact that I'm setting EntityState.Modified which marks all properties as modified. If I don't upload a new image, the ImageFilePath and ImageContentType coming from the front-end are effectively null which is what is getting stored. </p>
  
  <p>My question is how do I resolve this? What is the proper way to use EntityState.Modified?</p>