title: Existing rows in join table not being cleared during many-to-many update
body: <p>In the simplest terms, when I try to update my object, <code>Activities</code>, from POST data, the existing many-to-many join data for the relationship <code>Activities.Contacts</code> does not get removed from the MSSQL DB during <code>SaveChanges</code>.  This results in an error if one of the Contacts in the POST data already exists for that Activity. </p>
  
  <blockquote>
    <p>Violation of PRIMARY KEY constraint 'PK_tblActionContact'. Cannot insert duplicate key in object 'dbo.tblActionContact'. The duplicate key value is (1, 94)</p>
  </blockquote>
  
  <p>My controller for this:</p>
  
  <pre><code>    public ActionResult Edit(Activity activity)
      {
          if (ModelState.IsValid)
          {
              //save the activity
              activity.Contact.Clear(); //Clear the existing contacts
              db.Entry(activity).State = EntityState.Modified;
  
              //then add each contact
              foreach (int contactId in activity.SelectedContactIds)
              {
                  activity.Contact.Add(db.tblContact.Find(contactId));
              }
  
              db.SaveChanges();
              TempData["Flash Success"] = "Activity Saved";
              return RedirectToAction("Index", "Home");
          }
  
          return View(activity);
      }
  </code></pre>
  
  <p>I have tried oh so many different variations based on much searching of this fine site, but I have found nothing that will work.</p>
  
  <p>I had very high hopes for <code>activity.Contact.Clear()</code> and that does indeed clear the existing Contacts from the Activities in the object, but it does not clear them from the actual MSSQL table.</p>
  
  <p>If I am selecting novel contacts that were not selected in the existing <code>Activity</code>, then it saves fine, including of course when creating a new <code>Activity</code>.</p>
  
  <p>Please, I am new to posting here so please let me know if more code or info is needed!</p>
  
  <p><strong>Edit</strong>
  I am posting my context and applicable models:</p>
  
  <p>Context:</p>
  
  <pre><code>public partial class SalesEntities : DbContext
  {
      public SalesEntities()
          : base("name=SalesEntities")
      {
      }
  
      protected override void OnModelCreating(DbModelBuilder modelBuilder)
      {
          throw new UnintentionalCodeFirstException();
      }
  
      public virtual DbSet&lt;SalesAccount&gt; tblAccount { get; set; }
      public virtual DbSet&lt;Activity&gt; tblAction { get; set; }
      public virtual DbSet&lt;ActionTypeLookup&gt; tblActionTypeLookup { get; set; }
      public virtual DbSet&lt;Campaign&gt; tblCampaign { get; set; }
      public virtual DbSet&lt;Contact&gt; tblContact { get; set; }
      public virtual DbSet&lt;GenericLookup&gt; tblGenericLookup { get; set; }
      public virtual DbSet&lt;MarketingLetter&gt; tblMarketingLetter { get; set; }
      public virtual DbSet&lt;Opportunity&gt; tblOpportunity { get; set; }
      public virtual DbSet&lt;Product&gt; tblProduct { get; set; }
      public virtual DbSet&lt;Source&gt; tblSource { get; set; }
      public virtual DbSet&lt;VerifiedAccount&gt; tblVerifiedAccount { get; set; }
      public virtual DbSet&lt;AspNetUsers&gt; AspNetUsers { get; set; }
  }
  }
  </code></pre>
  
  <p>And the Activity model:</p>
  
  <pre><code>namespace SalesTracker.Models
  {
  using System;
  using System.Collections.Generic;
  
  public partial class Activity
  {
      public Activity()
      {
          this.Contact = new HashSet&lt;Contact&gt;();
      }
  
      public int ActionId { get; set; }
      public int AccountId { get; set; }
      public int CampaignId { get; set; }
      public string SalespersonId { get; set; }
      public int ActionTypeId { get; set; }
      public string Note { get; set; }
      public Nullable&lt;System.DateTime&gt; ActionDate { get; set; }
      public string NextStep { get; set; }
      public string Duration { get; set; }
      public Nullable&lt;int&gt; MarketingLetterId { get; set; }
      public string EditedBy { get; set; }
      public bool Active { get; set; }
      public System.DateTime EditDate { get; set; }
      public bool Completed { get; set; }
  
      public virtual SalesAccount SalesAccount { get; set; }
      public virtual ActionTypeLookup ActionTypeLookup { get; set; }
      public virtual Campaign Campaign { get; set; }
      public virtual MarketingLetter MarketingLetter { get; set; }
      public virtual ICollection&lt;Contact&gt; Contact { get; set; }
      public virtual AspNetUsers AspNetUsers { get; set; }
      public virtual AspNetUsers AspNetUsersEditedBy { get; set; }
  }
  }
  </code></pre>
  
  <p>And the Contact model</p>
  
  <pre><code>namespace SalesTracker.Models
  {
  using System;
  using System.Collections.Generic;
  
  public partial class Contact
  {
      public Contact()
      {
          this.Activity = new HashSet&lt;Activity&gt;();
      }
  
      public int ContactId { get; set; }
      public int AccountId { get; set; }
      public string FirstName { get; set; }
      public string LastName { get; set; }
      public string Title { get; set; }
      public string Phone { get; set; }
      public string Email { get; set; }
      public string Note { get; set; }
      public System.DateTime VerifiedDate { get; set; }
      public string VerifiedBy { get; set; }
      public int VerificationStatus { get; set; }
      public System.DateTime EditDate { get; set; }
      public string EditedBy { get; set; }
      public bool Active { get; set; }
  
      public virtual SalesAccount SalesAccount { get; set; }
      public virtual ICollection&lt;Activity&gt; Activity { get; set; }
      }
  }
  </code></pre>