title: Update relationships when saving changes of EF4 POCO objects
body: <p>Entity Framework 4, POCO objects and ASP.Net MVC2. I have a many to many relationship, lets say between BlogPost and Tag entities. This means that in my T4 generated POCO BlogPost class I have:</p>
  
  <pre><code>public virtual ICollection&lt;Tag&gt; Tags {
      // getter and setter with the magic FixupCollection
  }
  private ICollection&lt;Tag&gt; _tags;
  </code></pre>
  
  <p>I ask for a BlogPost and the related Tags from an instance of the ObjectContext and send it to another layer (View in the MVC application). Later I get back the updated BlogPost with changed properties and changed relationships. For example it had tags "A" "B" and "C", and the new tags are "C" and "D". In my particular example there are no new Tags and the properties of the Tags never change, so the only thing which should be saved is the changed relationships. Now I need to save this in another ObjectContext. (Update: Now I tried to do in the same context instance and also failed.)</p>
  
  <p>The problem: I can't make it save the relationships properly. I tried everything I found:</p>
  
  <ul>
  <li>Controller.UpdateModel and Controller.TryUpdateModel don't work.</li>
  <li>Getting the old BlogPost from the context then modifying the collection doesn't work. (with different methods from the next point)</li>
  <li><a href="https://stackoverflow.com/questions/2291980/how-to-save-child-relationship-entities-in-the-entity-framework">This</a> probably would work, but I hope this is just a workaround, not the solution :(.</li>
  <li>Tried Attach/Add/ChangeObjectState functions for BlogPost and/or Tags in every possible combinations. Failed.</li>
  <li><a href="http://www.codetuning.net/blog/post/Binding-Model-Graphs-with-ASPNETMVC.aspx" rel="noreferrer">This</a> looks like what I need, but it doesn't work (I tried to fix it, but can't for my problem).</li>
  <li>Tried ChangeState/Add/Attach/... the relationship objects of the context. Failed.</li>
  </ul>
  
  <p>"Doesn't work" means in most cases that I worked on the given "solution" until it produces no errors and saves at least the properties of BlogPost. What happens with the relationships varies: usually Tags are added again to the Tag table with new PKs and the saved BlogPost references those and not the original ones. Of course the returned Tags have PKs, and before the save/update methods I check the PKs and they are equal to the ones in the database so probably EF thinks that they are new objects and those PKs are the temp ones.</p>
  
  <p>A problem I know about and might make it impossible to find an automated simple solution: When a POCO object's collection is changed, that should happen by the above mentioned virtual collection property, because then the FixupCollection trick will update the reverse references on the other end of the many-to-many relationship. However when a View "returns" an updated BlogPost object, that didn't happen. This means that maybe there is no simple solution to my problem, but that would make me very sad and I would hate the EF4-POCO-MVC triumph :(. Also that would mean that EF can't do this in the MVC environment whichever EF4 object types are used :(. I think the snapshot based change tracking should find out that the changed BlogPost has relationships to Tags with existing PKs.</p>
  
  <p>Btw: I think the same problem happens with one-to-many relations (google and my colleague say so). I will give it a try at home, but even if that works that doesn't help me in my six many-to-many relationships in my app :(.</p>