title: ASP.NET MVC / EF4 / POCO / Repository - How to Update Relationships?
body: <p>I have a 1..* relationship between <strong>Review</strong> and <strong>Recommendations</strong>.</p>
  
  <p>The relevant portion of my model (which is also the POCO mapped by EF4):</p>
  
  <pre><code>public class Review
  {
     public ICollection&lt;Recommendations&gt; Recommendations { get; set; }
  }
  </code></pre>
  
  <p>On an <strong>Edit View</strong>, i represent the <em>Recommendations</em> as a set of checkboxes.</p>
  
  <p>When i try and <strong>add a new Recommendation</strong> as part of editing the Review (e.g check another box), nothing is happening - and i know why...</p>
  
  <p>I use the "stub technique" to update my entities - e.g i create a entity with the same key, attach it to the graph, then do <code>ApplyCurrentValues</code>. But this only works for scalar properties, not for navigational properties.</p>
  
  <p>I found <a href="https://stackoverflow.com/questions/3721755/update-non-scalar-entities-in-entity-framework-v4">this StackOverflow question</a> which looks good, but i am trying to work out how to get this to work with POCO's/Repository (and ASP.NET MVC - detached context).</p>
  
  <p>As i'm using POCO's, <code>review.Recommendations</code> is an <code>ICollection&lt;Recommendation&gt;</code>, so i can't do <code>review.Recommendations.Attach</code>. I'm not using Self-Tracking Entities either, so i need to manually work with the graph/change tracking - which hasn't been a problem until now.</p>
  
  <p><strong>So you can visualize the scenario:</strong></p>
  
  <p>Review:</p>
  
  <ul>
  <li>Recommendations (<code>ICollection&lt;Recommendation&gt;</code>):
  
  <ul>
  <li>RecommendationOne (<code>Recommendation</code>)</li>
  <li>RecommendationTwo (<code>Recommendation</code>)</li>
  </ul></li>
  </ul>
  
  <p>If im on the edit view, two of the checkboxes are already checked. The third one (representing RecommendationThree) is <strong>unchecked</strong>.</p>
  
  <p>But if i check that box, the above model becomes:</p>
  
  <p>Review:</p>
  
  <ul>
  <li>Recommendations (<code>ICollection&lt;Recommendation&gt;</code>):
  
  <ul>
  <li>RecommendationOne (<code>Recommendation</code>)</li>
  <li>RecommendationTwo (<code>Recommendation</code>)</li>
  <li>RecommendationThree (<code>Recommendation</code>)</li>
  </ul></li>
  </ul>
  
  <p>And so i need to attach RecommendationThree to the graph as a <strong>new entity</strong>.</p>
  
  <p>Do i need hidden fields to compare the posted data the existing entity? Or should i store the entity in TempData and compare that to the posted entity?</p>
  
  <p><strong>EDIT</strong></p>
  
  <p>To avoid confusion, here is the full app stack call:</p>
  
  <p><strong>ReviewController</strong></p>
  
  <pre><code>[HttpPost]
  public ActionResult Edit(Review review)
  {
     _service.Update(review); // UserContentService
     _unitOfWork.Commit();
  }
  </code></pre>
  
  <p><strong>UserContentService</strong></p>
  
  <pre><code>public void Update&lt;TPost&gt;(TPost post) where TPost : Post, new()
  {
     _repository.Update(post); // GenericRepository&lt;Post&gt;
  }
  </code></pre>
  
  <p><strong>GenericRepository - used as <code>GenericRepository&lt;Post&gt;</code></strong></p>
  
  <pre><code>public void Update&lt;T2&gt;(T2 entity) where T2 : class, new()
  {
     // create stub entity based on entity key, attach to graph.
  
     // override scalar values
     CurrentContext.ApplyCurrentValues(CurrentEntitySet, entity);
  }
  </code></pre>
  
  <p>So, the <code>Update</code> (or <code>Add</code> or <code>Delete</code>) Repository methods needs to be called for each recommendation, depending it's new/modified/deleted.</p>