title: EF Code First: Join table is empty
body: <p>I have two Entity Framework Code First POCOs. I can insert new Site and Technology items into the database easily, but I can't seem to associate Sites with Technologies. When I do this ...</p>
  
  <pre><code>if(!site.Technologies.Any( o =&gt; (o.Name == technology.Name &amp;&amp;  technology.Source == source)))
  {
    site.Technologies.Add(technology);
    technology.Sites.Add(site);
  }
  </code></pre>
  
  <p>I get a duplicate key error. If I remove the second line from the conditional, the join table TechnologiesSites does not get any records! How do I do an insert in EF Code First?</p>
  
  <pre><code>public class Site
  {
      public Site()
      {
          Technologies = new HashSet&lt;Technology&gt;();
      }
  
      [Key]
      public string Hostname { get; set; }
      public virtual ICollection&lt;Technology&gt; Technologies { get; set; }
  }
  </code></pre>
  
  <p>and</p>
  
  <pre><code>public class Technology
  {
      [Key, Column(Order = 0)]
      public string Name { get; set; }
      [Key, Column(Order = 1)]
      public string Source { get; set; }
      public virtual ICollection&lt;Site&gt; Sites { get; set; }
  
      public Technology()
      {
          Sites = new HashSet&lt;Site&gt;();
      }
  }
  </code></pre>