title: Entity Framework : many to many relationship , Insert and update
body: <p>I have a news entity and I get the news based on their NewsID. then I defined a new entity , a Group and I want to get the news based on their Group ID. I tried to handle this (many to many) relationships using <a href="http://www.codeproject.com/Articles/234606/Creating-a-Many-To-Many-Mapping-Using-Code-First" rel="nofollow noreferrer">this article</a>by using code first approach.</p>
  
  <p><img src="https://i.stack.imgur.com/bONdJ.png" alt="enter image description here"></p>
  
  <p>so in my context I added :</p>
  
  <pre><code> public class Groupnews : DbContext
      {
  
          public DbSet&lt;Group&gt; Group { get; set; }
          public DbSet&lt;News&gt; News { get; set; }
  
          public Groupnews()
              : base("MyDb")
          {
          }
  
          public int NewsID { get; set; }
      }
  
  this.HasMany(t =&gt; t.News)
      .WithMany(t =&gt; t.Groups)
      .Map(m =&gt;
          {
              m.ToTable("GroupNews");
              m.MapLeftKey("GroupID");
              m.MapRightKey("NewsID");
          });
  </code></pre>
  
  <p>now I can get the news based on Their GroupID using this approach. but the problem is in insertign new News and updating.For that I need to save NewsID and GroupId in GroupNews table. for doing this . in News model i defined :</p>
  
  <pre><code>    public virtual ICollection&lt;Group&gt; RelatedGroups { get; set; }
  
      public News()
      {
          RelatedGroups = new List&lt;Group&gt;();
      }
  </code></pre>
  
  <p>and the same for group :</p>
  
  <pre><code>    public virtual ICollection&lt;News&gt; RelatedNews { get; set; }
      public Group()
      {
          RelatedNews = new List&lt;News&gt;();
       }
  </code></pre>
  
  <p>In my news controller I add :</p>
  
  <pre><code>            Group group = new Group();
              group.RelatedNews.Add(news);
  </code></pre>
  
  <p><img src="https://i.stack.imgur.com/o3NxJ.png" alt="enter image description here"></p>
  
  <p>but nothing is updated and the NewsID is not adding to my GroupNews table .</p>