title: Why can't I update this foreign key property?
body: <p>I have two classes, here are the relevant bits:</p>
  
  <pre><code>public class Endorsement
  {
      public Guid? ContractId { get; set; }
      public virtual Contract Contract { get; set; }
  }
  
  public class Contract
  {
       public virtual ICollection&lt;Endorsement&gt; Sales { get; set; }
  }
  </code></pre>
  
  <p>The <code>Endorsement</code> class doesn't have a corresponding configuration, but the <code>Contract</code> class does:</p>
  
  <pre><code>public class ContractConfiguration : EntityTypeConfiguration&lt;Contract&gt;
  {
      public ContractConfiguration()
      {
           HasMany(a =&gt; a.Sales).WithRequired(a =&gt; a.Contract).HasForeignKey(a =&gt; a.ContractId);
      }
  }
  </code></pre>
  
  <p>I'm trying to change the <code>Contract</code> on an <code>Endorsement</code> to another <code>Contract</code>:</p>
  
  <pre><code>var endorsement = Context.Set&lt;Endorsement&gt;().Find(endorsementId);
  var newContract = Context.Set&lt;Contract&gt;().Find(newContractId);
  endorsement.Contract = newContract;
  /* Instead of the line above, I have also tried endorsement.ContractId = newContract.Id;
     I have also tried newContract.Sales.Add(endorsement); and then setting the state of 
     the newContract entry to modified and saving, all with the same results.
  */
  
  var entry = Context.Entry(endorsement);
  entry.State = EntityState.Modified;
  Context.SaveChanges();
  </code></pre>
  
  <p><code>Context.SaveChanges();</code> throws the following exception:</p>
  
  <blockquote>
    <p>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 value(s) of 'Contract.Id' on one end of a relationship do not match the property value(s) of 'Endorsement.ContractId' on the other end.</p>
  </blockquote>
  
  <p>Any idea what's causing this?</p>