title: Unable to Set Foreign Key to Null
body: <p>I have an optional foreign key that I'm attempting to set to null.  No matter what I've tried, on SaveChanges(), the update statement sets the foreign key to the previous value instead of null.</p>
  
  <p>Simplified Child Class:</p>
  
  <pre><code>public class Child
  {
      [Key, Column(Order = 0), ScaffoldColumn(false)]
      [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
      public int Id { get; set; }
  
      [ForeignKey("Parent")]
      public int? ParentId { get; set; }
  
      public virtual Parent Parent { get; set; }
  }
  </code></pre>
  
  <p>Simplified Parent Class:</p>
  
  <pre><code>public class Parent
  {
      [Key, Column(Order = 0), ScaffoldColumn(false)]
      [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
      public int Id { get; set; }
  
      public virtual ICollection&lt;Child&gt; Children { get; set; }
  }
  </code></pre>
  
  <p>Things I've tried:</p>
  
  <ol>
  <li>Load the Child object and set the ParentId null and set the Parent to null</li>
  <li>Load the Child object and set the ParentId null and force the entity state to be modified</li>
  <li>Load the Child object including the Parent object, then setting the values to null and forcing the entity state to be modified</li>
  <li>Load the Parent object, then the Child object and .Remove(child) from the Parent object</li>
  <li>Load the Parent object, then the Child object and .Remove(child) from the Parent and set the Child.ParentId to null and the Child.Parent to null.</li>
  </ol>
  
  <p>Currently I have:</p>
  
  <pre><code>public void RemoveChildFromParent(int childId, int parentId)
  {
      Parent parent = _context.Parents.Include(x =&gt; x.Children).FirstOrDefault(u =&gt; u.Id == parentId);
      Child child = parent.Children.SingleOrDefault(u =&gt; u.Id == childId);
      parent.Children.Remove(child);
      child.ParentId = null;
      child.Parent = null;
      child.StateOfEntity = StateOfEntity.Modified;
  
      _context.ApplyStateChanges();
      _context.SaveChanges();
  }
  </code></pre>
  
  <p>On Save Changes, the SQL Update Statement still sets the ParentId on the Child object to the old value and I get this error:</p>
  
  <pre><code>System.InvalidOperationException was unhandled by user code
    HResult=-2146233079
    Message=The changes to the database were committed successfully, but an error occurred while updating the object context. The ObjectContext might be in an inconsistent state. Inner exception message: A referential integrity constraint violation occurred: The property values that define the referential constraints are not consistent between principal and dependent objects in the relationship.
    Source=System.Data.Entity
    StackTrace:
         at System.Data.Objects.ObjectContext.SaveChanges(SaveOptions options)
         at System.Data.Entity.Internal.InternalContext.SaveChanges()
         at System.Data.Entity.Internal.LazyInternalContext.SaveChanges()
         at System.Data.Entity.DbContext.SaveChanges()
         at Insight.DataLayer.InsightContext.SaveChanges()
         at Insight.DataLayer.ChildRepository.RemoveChildFromParent(Int32 childId, Int32 parentId)
         at Insight.BusinessLayer.ParentManager.RemoveChild(Int32 id, Int32 parentId)
         at Insight.PresentationLayer.Controllers.ParentController.RemoveChild(Int32 id, Int32 parentId)
         at lambda_method(Closure , ControllerBase , Object[] )
         at System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary`2 parameters)
         at System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary`2 parameters)
         at System.Web.Mvc.Async.AsyncControllerActionInvoker.&lt;&gt;c__DisplayClass37.&lt;&gt;c__DisplayClass39.&lt;BeginInvokeActionMethodWithFilters&gt;b__33()
         at System.Web.Mvc.Async.AsyncControllerActionInvoker.&lt;&gt;c__DisplayClass4f.&lt;InvokeActionMethodFilterAsynchronously&gt;b__49()
    InnerException: 
  </code></pre>
  
  <p>Also, not sure if it matters, but I have LazyLoadingEnabled = false and AutoDetectChangesEnabled = false.</p>