title: Adding and deleting many-to-many using DbContext API
body: <p>I am using Entity Framework and DbContext API do build my application but I am having trouble working with objects with many-to-many relations. A simplified save-method could look like this</p>
  
  <pre><code>public void MyObj_Save(MyObj myobj)
  {
    DbContext.Entry(myobj).State = EntityState.Added;
    DbContext.SaveChanges();
  }
  </code></pre>
  
  <p>This code works fine, but if MyObj contains a many-to-many relation this is not saved. I know from using the old POCO API, that I needed to attach the related objects to the context but I cannot find a way to do this correctly with the DbContext API - a simplified example below</p>
  
  <pre><code>public void MyObj_Save(MyObj myobj, List&lt;OtherObj&gt; otherObjList)
  {
    foreach (OtherObj otherObj in otherObjList)
    {
      DbContext.OtherObj.Attach(otherObj);
      myobj.OtherObj.Add(otherObj);
    }
  
    DbContext.Entry(myobj).State = EntityState.Added;
    DbContext.SaveChanges();
  }
  </code></pre>
  
  <p>I get no error, but the relations are not saved. What to do?</p>