title: Entity Framework 5 does not clear navigation property
body: <p>I've got this strange issue with Entity Framework 5 where I have a navigation property in one of my entities that I would like to set to <code>null</code>. But for some reason the property only gets cleared the second time I call this property:</p>
  
  <pre><code>using (var db = new Entities())
  {
      var vehicle = db.Vehicles.Single(v =&gt; v.Id == vehicleId);
  
      // After this call, ParkingBay is still set.
      vehicle.ParkingBay = null;
  
      // Only after this call, ParkingBay becomes null.
      vehicle.ParkingBay = null;
  
      db.SaveChanges();
  }
  </code></pre>
  
  <p>The <code>Vehicle</code> class that Entity Framework generates looks like this:</p>
  
  <pre><code>public partial class Vehicle
  {
      public int Id { get; set; }
      public System.DateTime CreatedOn { get; set; }
      public int CreatedBy { get; set; }
      public virtual ParkingBay ParkingBay { get; set; }
  }
  </code></pre>
  
  <p>This code isn't very exiting, and at runtime Entity Framework generates proxy classes for both <code>Vehicle</code> and <code>ParkingBay</code>, but I can't understand what happens inside that <code>ParkingBay</code> property that fails to clear the property on the first call.</p>
  
  <p>Between <code>Vehicle</code> and <code>ParkingBay</code> is a normal Foreign Key relationship in SQL Server. Nothing special here.</p>
  
  <p><strong>UPDATE</strong></p>
  
  <p>The <code>ParkingBay</code> looks like this:</p>
  
  <pre><code>public partial class ParkingBay
  {
      public ParkingBay()
      {
          this.Vehicles = new HashSet&lt;Vehicle&gt;();
      }
  
      public int Id { get; set; }
      public System.DateTime CreatedOn { get; set; }
      public int CreatedBy { get; set; }
  
      public virtual ICollection&lt;Vehicle&gt; Vehicles { get; set; }
  }
  </code></pre>
  
  <p>What's going on here? Can someone enlighten me what I'm doing wrong?</p>