title: Cannot get relationship to update for navigation properties in entity framework
body: <p>I am currently using EF4.3 and Code First. Creation of my objects works (via my views - just using the auto-generated Create), but when I attempt to edit an object, it does not save any changes that, utlimately, tie back to my navigation properties. I have been <a href="http://www.ef-faq.org/relationships.html" rel="noreferrer">reading on relationships</a>, but I don't understand how to tell my context that the relationship has changed.</p>
  
  <p>Here is some example code of my implementation.</p>
  
  <pre><code>@* Snippet from my view where I link into my ViewModel. *@
  &lt;div class="row"&gt;
      &lt;div class="editor-label"&gt;
          @Html.LabelFor(model =&gt; model.ManagerID)
      &lt;/div&gt;
      &lt;div class="editor-field"&gt;
          @Html.DropDownListFor(model =&gt; model.ManagerID, ViewBag.Manager as SelectList, String.Empty)
          @Html.ValidationMessageFor(model =&gt; model.ManagerID)
      &lt;/div&gt;
  &lt;/div&gt;
  </code></pre>
  
  <p>Here is my Controller implementation (POST of my Edit):</p>
  
  <pre><code>    [HttpPost]
      public ActionResult Edit(ProjectViewModel projectViewModel)
      {
          if (ModelState.IsValid)
          {
              Project project = new Project();
              project.ProjectID = projectViewModel.ProjectID;
              project.Name = projectViewModel.Name;
              project.ProjectManager = repository.GetUser(projectViewModel.ManagerID);
              repository.InsertOrUpdateProject(project);
              repository.Save();
              return RedirectToAction("Index");
          }
          ViewBag.Manager = new SelectList(repository.GetUsers(), "UserID", "FullName", projectViewModel.ManagerID);
          return View(projectViewModel);
      }
  </code></pre>
  
  <p>Within my Project object:</p>
  
  <pre><code>public class Project
  {
      public int ProjectID { get; set; }
      [Required]
      public string Name { get; set; }
  
      // Navigation Properties
      public virtual User Manager { get; set; }
  }
  </code></pre>
  
  <p>Here is the corresponding method from the repository (where my context resides):</p>
  
  <pre><code>public void InsertOrUpdateProject(Project project)
      {
          if (program.ProjectID == default(int))
          {
              context.Projects.Add(project);
          }
          else
          {
              context.Entry(project).State = EntityState.Modified;
          }
      }
  </code></pre>
  
  <p>Just to be clear, this does work to update my properties, but it does not update my navigation properties (in this case, Manager). Appreciate any help.</p>