title: CodeFirst - Update single property
body: <p>We are using EF5, Code First approach to an MVC4 app that we're building.  We are trying to update 1 property on an entity but keep getting errors.  Here's what the class looks like which the context created:</p>
  
  <pre><code>public partial class Room
  {
      public Room()
      {
          this.Address = new HashSet&lt;Address&gt;();
      }
  
      public int RoomID { get; set; }
      public Nullable&lt;int&gt; AddressID { get; set; }
      public Nullable&lt;int&gt; ProductVersionID { get; set; }
      public string PhoneNumber { get; set; }
      public string AltPhone { get; set; }
      public string RoomName { get; set; }
      public string Description { get; set; }
      public string Comments { get; set; }
      public string Notes { get; set; }
  
      public virtual ICollection&lt;Address&gt; Address { get; set; }
  }
  </code></pre>
  
  <p>Here's our ViewModel for the view:</p>
  
  <pre><code>public class RoomDetailsViewModel
  {
      //public int RoomID { get; set; }
      public string RoomName { get; set; }
      public string PhoneNumber { get; set; }
      public string AltPhone { get; set; }
      public string Notes { get; set; }
      public string StateCode { get; set; }
      public string CountryName { get; set; }
      public string ProductVersion { get; set; }
      public int PVersionID { get; set; }
      public List&lt;SelectListItem&gt; ProductVersions { get; set; }
      public Room Room { get; set; }
  }
  </code></pre>
  
  <p>Here's the Controller Action being called on "Save":</p>
  
  <pre><code>    [HttpPost]
      public virtual ActionResult UpdateRoom(RoomDetailsViewModel model)
      {
          var db = new DBContext();
  
          bool b = ModelState.IsValid;
  
          var rooms = db.Rooms;
          var rm = rooms.Where(r =&gt; r.RoomID == model.Room.RoomID).Single();
          //List&lt;Address&gt; address = db.Addresses.Where(a =&gt; a.AddressID == rm.AddressID).ToList&lt;Address&gt;();
  
          rm.ProductVersionID = model.PVersionID;
          //rm.Address = address;
  
          db.Entry(rm).Property(r =&gt; r.ProductVersionID).IsModified = true;
  
          //db.Entry(rm).State = System.Data.EntityState.Modified;
  
          db.SaveChanges();
  
          return View("RoomSaved", model);
      }
  </code></pre>
  
  <p>All this view does is display data and allow the user to change the Product Version (from a SelectList), so, in the Room Entity, all we are updating is the ProductVersionID property, nothing else.  We can get the data to display properly but when we click "save", we get this error:</p>
  
  <blockquote>
    <p>An object of type 'System.Collections.Generic.List`1[[Models.Address,
    Web.Mobile.TestSite, Version=1.0.0.0, Culture=neutral,
    PublicKeyToken=null]]'  cannot be set or removed from the Value
    property of an EntityReference of type 'Models.Address'.</p>
  </blockquote>
  
  <p>As you can see by the Controller Action, we've tried several different things but all seem to produce this error.  I've tried to populate the model.Room.Address collection with an Address, without, but still get this error.</p>
  
  <p>I read <a href="https://stackoverflow.com/questions/5564727/update-a-single-property-of-a-record-in-entity-framework-code-first">this StackOverflow article</a> and <a href="https://stackoverflow.com/questions/11502840/unable-to-set-field-property-on-entity-type-with-entity-framework-4-3-1">this article as well</a> but neither have solved my problem.</p>
  
  <p>ANY help with this would be greatly appreciated!</p>