title: ASP.NET MVC Entity Framework 6 Navigation Property not saving on SaveChanges() Repository Pattern on Edit
body: <p>I am having a strange problem where a navigation property is not persisiting on the Update of a page, but is on a Create.
  I am using a Generic Repository pattern with Entity Framework 6, ASP.NET MVC 5.</p>
  
  <p>Here's the skeleton code:</p>
  
  <p>My context:</p>
  
  <pre><code>public class ApplicationDbContext : IdentityDbContext&lt;ApplicationUser&gt;
  {
      public ApplicationDbContext() : base("DefaultConnection") { }       
  }
  </code></pre>
  
  <p>My repository implementation:</p>
  
  <pre><code>public class EntityFrameworkRepository : IRepository, IDisposable
  {
      private ApplicationDbContext _context;
      private readonly ConcurrentDictionary&lt;Type, object&gt; _dbSets =
          new ConcurrentDictionary&lt;Type, object&gt;();
  
      public EntityFrameworkRepository(ApplicationDbContext context)
      {
          _context = context;
      }
  
      public void Save&lt;T&gt;(T entity) where T : BaseModel
      {
          GetDbSet&lt;T&gt;().Add(entity);
          _context.SaveChanges();
      }
  
      public void Update&lt;T&gt; (T entity) where T: BaseModel
      {
          _context.Entry(entity).State = EntityState.Modified;
          _context.SaveChanges();
      }
  }
  </code></pre>
  
  <p>Here the basis of Get of the Edit page uses AutoMapper to map between the domain model and the view model.</p>
  
  <pre><code>public ActionResult Edit(int menuId, int? id)
  {
     MenuItem mi = _repository.Get&lt;MenuItem&gt;((int)id);
     Mapper.CreateMap&lt;MenuItem, MenuItemViewModel&gt;();
     MenuItemViewModel mivm = Mapper.Map&lt;MenuItem, MenuItemViewModel&gt;(mi);
  ///....///
  }
  </code></pre>
  
  <p>This is the method that is not working. As you can see, the full object UseSiblingClaim is being retrieved by id and set to the MenuItem itemToSave. I check the object just before _repository.Update() is called, and the property correctly.</p>
  
  <pre><code>[HttpPost]
  [ValidateAntiForgeryToken]
  public ActionResult Edit(int menuId, [Bind(Include = "Id,Name,ControllerName,ActionName,Order,CustomUrl,Tooltip,IconClass,CanUseParentsClaim,DoesNotNeedClaim,UseSiblingClaim_Id")] MenuItemViewModel mivm)
  {
      if (ModelState.IsValid)
      {
          mivm.Menu = _repository.Get&lt;Menu&gt;(menuId);
  
          if (mivm.UseSiblingClaim_Id != null)
          {
             mivm.UseSiblingClaim = _repository.Get&lt;MenuItem&gt;((int)mivm.UseSiblingClaim_Id);
          }
  
          MenuItem itemToSave = MapViewModelToModel(mivm, _repository);
          _repository.Update&lt;MenuItem&gt;(itemToSave);
          return RedirectToAction("Details", "Menu", new { Id = menuId });
       }
       return View(mivm);
   }
  </code></pre>
  
  <p>Here's the MenuItem class:</p>
  
  <pre><code>public class MenuItem : BaseModel
  {
      public MenuItem()
      {
          Children = new HashSet&lt;MenuItem&gt;();
      }        
  
      [Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
      public int Id { get; set; }        
      public virtual MenuItem ParentMenuItem { get; set; }
      public virtual Menu Menu { get; set; }        
      public string Name { get; set; }
      ///.....///
      public virtual MenuItem UseSiblingClaim { get; set; }
  }
  </code></pre>
  
  <p>The mapping method is here: UseSiblingClaim is being set.</p>
  
  <pre><code>private static MenuItem MapViewModelToModel(MenuItemViewModel mivm, IRepository _repository)
      {
  
  
          MenuItem itemToSave = new MenuItem()
          {
              UseSiblingClaim = mivm.UseSiblingClaim,
              Id = mivm.Id,
              ActionName = mivm.ActionName,
              CanUseParentsClaim = mivm.CanUseParentsClaim,
              ControllerName = mivm.ControllerName,
              CustomURL = mivm.CustomURL,
              DoesNotNeedClaim = mivm.DoesNotNeedClaim,
              IconClass = mivm.IconClass,
              Menu = mivm.Menu,
              Name = mivm.Name,
              Order = mivm.Order,
              ParentMenuItem = mivm.ParentMenuItem,
              Tooltip = mivm.Tooltip,                     
          };
          return itemToSave;
      }
  </code></pre>
  
  <p>Here's the view model:</p>
  
  <pre><code>public class MenuItemViewModel : BaseModel
      {
          public int Id { get; set; }
          public int ParentMenuItem_Id { get; set; }
          public MenuItem ParentMenuItem { get; set; }
          public int Menu_Id { get; set; }
          public Menu Menu { get; set; }
          public IEnumerable&lt;SelectListItem&gt; SiblingItems { get; set; }
          [DisplayName("Can Use Sibling's Claim")]
          public int? UseSiblingClaim_Id { get; set; }
          public MenuItem UseSiblingClaim { get; set; }
      }
  </code></pre>
  
  <p>The View part (this is the same for Create() and Update(): </p>
  
  <pre><code>&lt;div class="form-group"&gt;
      @Html.LabelFor(model =&gt; model.UseSiblingClaim_Id, new { @class = "control-label col-md-2" })
      &lt;div class="col-md-10"&gt;
          @Html.DropDownListFor(x =&gt; x.UseSiblingClaim_Id, Model.SiblingItems, new { @class = "select2", @multiple = "multiple", @style = "width:100%" })
          @Html.ValidationMessageFor(model =&gt; model.UseSiblingClaim_Id)
      &lt;/div&gt;
  &lt;/div&gt;
  </code></pre>
  
  <p>Model.SiblingItems is created and passed into the view.</p>
  
  <p>Why won't the UseSiblingClaim navigation property be persisted? It works on Create of the Menu Item, but not on Update? Is it something to do with me uaing a generic repositroy pattern? Please help</p>