title: Update method in generic repository does not update navigational properties
body: <p>I would like to know if entity framework automatically tracks changes in navigational properties for an entity and also updates those changes when the related entity is updated using SaveChanges(). I am using a generic repository, unit of work, ninject with code first entity framework 5 in MVC 4. I have a post and tags tables that has a many-to-many relationship. I have a join table for this relationship, named PostTagMap, with two columns, Post_id and Tag_id. When I add a new post with its associated tags, the new post is successfully added to the post table and the PostTagMap also saves the tags associated with this post. However, when I try to edit/update the post and its associated tags, only the scalar properties of the post gets updated and not the associated tags. I tried debugging and I find that the post entity to be updated does have the updated values for the tags navigational property but entity framework only generates the update statement for the post table and not for the PostTagMap. What should I do to update the tags navigational property for the post as well.</p>
  
  <p>Following are the classes used </p>
  
  <p>DBContext class</p>
  
  <pre><code>public class EFDbContext : DbContext
  {
      public DbSet&lt;Post&gt; Posts { get; set; }
      public DbSet&lt;Tag&gt; Tags { get; set; }
  
      protected override void OnModelCreating(DbModelBuilder modelBuilder)
      {
          modelBuilder.Entity&lt;Post&gt;()
              .HasMany&lt;Tag&gt;(p =&gt; p.Tags)
              .WithMany(u =&gt; u.Posts)
              .Map(m =&gt;
              {
                  m.ToTable("PostTagMap");
                  m.MapLeftKey("Post_id");
                  m.MapRightKey("Tag_id");
              });
      }
  
   }
  </code></pre>
  
  <p>Post class</p>
  
  <pre><code>[Table("Post")]
  public class Post
  {
      public int Id { get; set; }
      public string Title { get; set; }
  
      public virtual ICollection&lt;Tag&gt; Tags { get; set; }  
  }
  </code></pre>
  
  <p>Tag class</p>
  
  <pre><code>[Table("Tag")]
  public class Tag
  {
      public int Id { get; set; }
      public string Name { get; set; }
  
      [JsonIgnore] 
      public  ICollection&lt;Post&gt; Posts { get; set; }
  }
  </code></pre>
  
  <p>Generic repository that implements IGenericRepository</p>
  
  <pre><code>public class EFGenericRepository&lt;T&gt; : IGenericRepository&lt;T&gt; where T : class
  {
      private DbContext DbContext { get; set; }
  
      private DbSet&lt;T&gt; DbSet { get; set; }
  
  
      public virtual void Add(T newEntity)
      {
          DbSet.Add(newEntity);
      }
  
      public virtual void Update(T entityToUpdate)
      {
          DbSet.Attach(entityToUpdate);
  
          DbContext.Entry(entityToUpdate).State = EntityState.Modified;
      }
  
   }
  </code></pre>
  
  <p>Unit of work that implements IUnitOfWork</p>
  
  <pre><code>public class EfUnitOfWork : IUnitOfWork
  {
      private EFDbContext dbContext;
  
      private IGenericRepository&lt;Post&gt; postRepository;
          private IGenericRepository&lt;Tag&gt; tagRepository;
  
      public EfUnitOfWork()
          {
              this.dbContext = new EFDbContext();
          }
  
      public void Commit()
          {
              dbContext.SaveChanges();
          }
  
  
      public IGenericRepository&lt;Post&gt; PostRepository
          {
              get
              {
                  if (this.postRepository == null)
                  {
                      this.postRepository = new EFGenericRepository&lt;Post&gt;(dbContext);
                  }
  
                  return postRepository;
              }
          }
  
      public IGenericRepository&lt;Tag&gt; TagRepository
          {
              get
              {
                  if (this.tagRepository == null)
                  {
                      this.tagRepository = new EFGenericRepository&lt;Tag&gt;(dbContext);
                  }
  
                  return tagRepository;
              }
          }
  
      private bool disposed = false;
  
          protected virtual void Dispose(bool disposing)
          {
              if (!this.disposed)
              {
                  if (disposing)
                  {
                      dbContext.Dispose();
                  }
              }
              this.disposed = true;
          }
  
          public void Dispose()
          {
              Dispose(true);
              GC.SuppressFinalize(this);
          }
  
  }
  </code></pre>
  
  <p>service layer </p>
  
  <pre><code>public class ServiceRepository : IServiceRepository
  {
      private readonly IUnitOfWork UoW;
  
      public ServiceRepository(IUnitOfWork _UoW)
          {
              UoW = _UoW;
          }
  
  
      public int AddPost(Post post)
          {
              UoW.PostRepository.Add(post);
  
              UoW.Commit();
  
              return post.Id;
          }
  
      public void EditPost(Post post)
          {
              UoW.PostRepository.Update(post);
  
              UoW.Commit();
          }
  
  }
  </code></pre>
  
  <p>admin controller</p>
  
  <pre><code>public class AdminController : Controller
  {
      private readonly IServiceRepository _serviceRepository;
  
      public AdminController(IServiceRepository serviceRepository)
      {
          _serviceRepository = serviceRepository;
      }       
  
  
  [HttpPost, ValidateInput(false)]
      public ContentResult AddPost(Post post)
      {
          string json;
  
          ModelState.Clear();
  
          if (TryValidateModel(post))
          {
              var id = _serviceRepository.AddPost(post);
  
              json = JsonConvert.SerializeObject(new
                                                  {
                                                      id = id,
                                                      success = true,
                                                      message = "Post added successfully"
                                                  });
          }
          else
          {
              json = JsonConvert.SerializeObject(new
              {
                  id = 0,
                  success = false,
                  message = "Post was not created"
              });
          }
  
          return Content(json, "application/json");
      }
  
  
      [HttpPost, ValidateInput(false)]
      public ContentResult EditPost(Post post)
      {
          string json;
  
          ModelState.Clear();
  
          if (TryValidateModel(post))
          {
              _serviceRepository.EditPost(post, Tags);
  
              json = JsonConvert.SerializeObject(new
                                                  {
                                                      id = post.Id,
                                                      success = true,
                                                      message = "Post updated successfully"
                                                  });
          }
          else
          {
              json = JsonConvert.SerializeObject(new
              {
                  id = 0,
                  success = false,
                  message = "Failed to save the changes."
              });
  
          }
  
          return Content(json, "application/json");
      }
   }
  </code></pre>
  
  <p>I am also using a custom model binder for Post which is as follows</p>
  
  <pre><code>public class PostModelBinder : DefaultModelBinder
  {
      private readonly IKernel _kernel;
  
          public PostModelBinder(IKernel kernel)
          {
              _kernel = kernel;
          }
  
      public override object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
          {
              var post = (Post)base.BindModel(controllerContext, bindingContext);
  
              var _blogRepository = _kernel.Get&lt;IServiceRepository&gt;();
  
      }
  
      var tags = bindingContext.ValueProvider.GetValue("Tags").AttemptedValue.Split(',');
  
      if (tags.Length &gt; 0)
          {
                  post.Tags = new List&lt;Tag&gt;();
  
                  foreach (var tag in tags)
                  {
                      post.Tags.Add(_serviceRepository.Tag( int.Parse(tag.Trim()) ));
                  }
          }
  
      return post;
  }
  </code></pre>