title: Add object to table containing List of other objects using Entity Framework
body: <p>I'm adding an object to a table in the database, adding a <code>contact</code> of <code>MarketingContact</code> type. These are my model classes:</p>
  
  <pre><code>public class MarketingContact
      {
          public MarketingContact()
          {
              this.ContactTags = new List&lt;ContactTag&gt;();
          }
  
          public int id { get; set; }
         //..some other properties not complex properties...
          public virtual ICollection&lt;ContactTag&gt; ContactTags { get; set; }
      }
  </code></pre>
  
  <p>This is the ContactTag model:</p>
  
  <pre><code> public class ContactTag
      {
          public ContactTag()
          {
              this.MarketingContacts = new List&lt;MarketingContact&gt;();
          }
  
          public int TagId { get; set; }
         //some other no complex properties...
          public virtual ICollection&lt;MarketingContact&gt; MarketingContacts { get; set; }
      }
  </code></pre>
  
  <p>And this is my <code>AddContact()</code> method in my controller:</p>
  
  <pre><code> [HttpPost]
          public ActionResult AddContact(MarketingVM marketingContactVM)
          {            
              List&lt;ContactTag&gt; contactTags = new List&lt;ContactTag&gt;();
              for (int i = 0; i &lt; marketingContactVM.MarketingContacts.Count; i++)
              {
                  if (marketingContactVM.MarketingContacts[i].IsSelected==true)
                  {
                      contactTags.Add(new ContactTag { Name = marketingContactVM.MarketingContacts[i].Name, TagId = marketingContactVM.MarketingContacts[i].TagId,
                      InfusionSoftId = marketingContactVM.MarketingContacts[i].InfusionSoftId});
                  }
              }
              MarketingContact marketingContact = AutoMapper.Mapper.Map&lt;MarketingContact&gt;(marketingContactVM);
              marketingContact.ContactTags = contactTags;                  
               context.MarketingContacts.Add(marketingContact);
               context.SaveChanges();          
              return Json(new { result = "success"}, JsonRequestBehavior.AllowGet);
          }    
  </code></pre>
  
  <p>Note that in my controller action I'm making use of a ViewModel(<code>MarketingVM type</code>).
  At the time that I save into the database the <code>MarketingContact</code> object is saved in the <code>MarketingContacts</code> table, but in the <code>ContactTags</code> table new rows are created depending on what the <code>ContactTags ICollection</code> that belong to <code>marketingContact</code> contains, and I'm just selecting from <code>ContactTags</code>, the rows are already there, I don't need more rows with same values added in <code>ContactTags</code> table, why is this happening? At the moment I save the object into the database the <code>marketingContact</code> object contains a <code>ContactTags</code> collection with fields existing in the <code>ContactTags</code> table, including the correct <code>TagId</code> values, but instead of just relate them to that row in the <code>MarketingContacs</code> table it create new rows in the <code>ContactTags</code> table with the same values except for the <code>TagId</code>(it generates a new value) therefore the values are duplicated everytime in the <code>ContactTags</code> table and I don't need that. What would I need to change?</p>