title: Entity Framework does not update Foreign Key object
body: <p>I'm new to Entity Framework and this behavior confuses me:</p>
  
  <pre><code>    [Table("ClinicProfile")]
      public class ClinicProfile
      {
          [Key]
          [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
          public int Id { get; set; }
  
          [ForeignKey("ContactData")]
          public int ContactDataId { get; set; }
          public ContactData ContactData { get; set; }
      }
  
      [Table("ContactData")]
      public class ContactData
      {
           [Key]
           [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
           public int Id { get; set; }
  
           ...
      }
  </code></pre>
  
  <p>When inserting the new entity all works fine - ContactData is saved to the table and foreign key assigned: </p>
  
  <pre><code>clinicProfile.ContactData = contactData;
  SharedContext.Current.Entry(clinicProfile).State = EntityState.Added;
  SharedContext.Current.SaveChanges();
  </code></pre>
  
  <p>But when I try to update this entity, ContactData don't get an update.</p>
  
  <pre><code>clinicProfile.ContactData = contactData;
  SharedContext.Current.Entry(clinicProfile).State = EntityState.Modified;
  SharedContext.Current.SaveChanges();
  </code></pre>
  
  <p>Must I mark ContactData as modified too? Or am I just doing something wrong?</p>
  
  <p><strong>EDIT-2 - The answer</strong></p>
  
  <p>Use this code, if contactData is the new object in the DB, with the new object id.</p>
  
  <pre><code>clinicProfile.ContactData = contactData;
  SharedContext.Current.Entry(clinicProfile).State = EntityState.Modified;
  SharedContext.Current.SaveChanges();
  </code></pre>
  
  <p>If you just want to update the old contactData, it would be correct to use this code:</p>
  
  <pre><code>SharedContext.Current.Entry(contactData).State = EntityState.Modified;
  SharedContext.Current.SaveChanges();
  </code></pre>
  
  <p><strong>EDIT - extended code snapshot</strong></p>
  
  <p>Code from MVC controller, postback from the page. Parameters "clinicProfile" and "contactData", "adressData" contain valid Id's.</p>
  
  <pre><code>        [HttpPost]
          [ValidateAntiForgeryToken]
          public ActionResult Modify(ClinicProfile clinicProfile, ContactData contactData, AdressData adressData)
          {
              ViewBag.Id = clinicProfile.Id;
  
              if (ModelState.IsValid)
              {
                  if (clinicProfile.Id != 0)
                  {
                      clinicProfile.ContactData = contactData;
                      clinicProfile.AdressData = adressData;
                      clinicProfile.AdressDataComposed = adressData.ComposeData();
                      clinicProfile.ContactDataComposed = contactData.ComposeData();
  
                      SharedContext.Current.Entry(clinicProfile).State = EntityState.Modified;
                      SharedContext.Current.SaveChanges();
  
                      Config.SaveClinicPhoto(clinicProfile.ClinicImageUpload, clinicProfile.Id);
                      Config.SaveClinicPreviewPhoto(clinicProfile.ClinicImageUpload, clinicProfile.Id);
  
                      return View(new ClinicProfileComposite { AdressData = adressData, ClinicProfile = clinicProfile, ContactData = contactData });
                  }
  
                  {
                      clinicProfile.ContactData = contactData;
                      clinicProfile.AdressData = adressData;
                      clinicProfile.AdressDataComposed = adressData.ComposeData();
                      clinicProfile.ContactDataComposed = contactData.ComposeData();
  
                      SharedContext.Current.Entry(clinicProfile).State = EntityState.Added;
                      SharedContext.Current.SaveChanges();
  
                      Config.SaveClinicPhoto(clinicProfile.ClinicImageUpload, clinicProfile.Id);
                      Config.SaveClinicPreviewPhoto(clinicProfile.ClinicImageUpload, clinicProfile.Id);
  
                      return RedirectToAction("Info", new { id = clinicProfile.Id });
                  }
              }
  
              ViewBag.Id = clinicProfile.Id;
              return View(new ClinicProfileComposite { AdressData = adressData, ClinicProfile = clinicProfile, ContactData = contactData });
          }
  </code></pre>