title: Entity Framework Update Entity along with child entities (add/update as necessary)
body: <p>I have a many-to-many relationship between <code>Issues</code> and <code>Scopes</code> in my EF Context.  In ASP.NET MVC, I bring up an Edit form that allows the user to edit a particular Issue.  At the bottom of the form, is a list of checkboxes that allow them to select which scopes apply to this issue.  When editing an issue, it likely will always have some Scopes associated with it already--these boxes will be checked already.  However, the user has the opportunity to check more scopes or remove some of the currently checked scopes.  My code looked something like this to save <strong>just the Issue</strong>:</p>
  
  <pre><code>            using (var edmx = new MayflyEntities())
              {
                  Issue issue = new Issue { IssueID = id, TSColumn = formIssue.TSColumn };
                  edmx.Issues.Attach(issue);
  
                  UpdateModel(issue);
  
                  if (ModelState.IsValid)
                  {
                      //if (edmx.SaveChanges() != 1) throw new Exception("Unknown error. Please try again.");
                      edmx.SaveChanges();
                      TempData["message"] = string.Format("Issue #{0} successfully modified.", id);
                  }
              }
  </code></pre>
  
  <p>So, when I try to add in the logic to save the associated scopes, I tried several things, but ultimately, this is what made the most sense to me:</p>
  
  <pre><code>            using (var edmx = new MayflyEntities())
              {
                  Issue issue = new Issue { IssueID = id, TSColumn = formIssue.TSColumn };
                  edmx.Issues.Attach(issue);
  
                  UpdateModel(issue);
  
                  foreach (int scopeID in formIssue.ScopeIDs)
                  {
                      var thisScope = new Scope { ID = scopeID };
                      edmx.Scopes.Attach(thisScope);
                      thisScope.ProjectID = formIssue.ProjectID;
                      if (issue.Scopes.Contains(thisScope))
                      {
                          issue.Scopes.Attach(thisScope); //the scope already exists
                      }
                      else
                      {
                          issue.Scopes.Add(thisScope); // the scope needs to be added
                      }
                  }
  
                  if (ModelState.IsValid)
                  {
                      //if (edmx.SaveChanges() != 1) throw new Exception("Unknown error. Please try again.");
                      edmx.SaveChanges();
                      TempData["message"] = string.Format("Issue #{0} successfully modified.", id);
                  }
              }
  </code></pre>
  
  <p>But, unfortunately, that just throws the following exception:</p>
  
  <pre><code>An object with the same key already exists in the ObjectStateManager. The ObjectStateManager cannot track multiple objects with the same key.
  </code></pre>
  
  <p>What am I doing wrong?</p>