title: Violation of primary key constraints, Cannot insert a duplicate key - In new empty tables
body: <p>I know this is a common issue and I've read about it <a href="https://stackoverflow.com/search?q=Violation%20Of%20primary%20key%20constraints,%20Cannot%20insert%20a%20duplicate">here</a> and elsewhere but in most cases the problem is that a row already exists in the database (which I'm checking for...). In my case it's a new db with empty tables (well.. except for Publishers table that has one row in <code>Id = 0</code>), the code goes like this (Entity Framework V5):</p>
  
  <pre><code>public void InsertPublisherResult(PublisherResult pResult)
  {
      using (mydbEntities e = new mydbEntities())
      {
          if (e.Publishers.Any(c =&gt; c.Name == pResult._Publisher.Name))
              continue;
  
          e.Publishers.Add(scraperResult._Publisher);
  
          foreach (RelatedPublisher rp in pResult._RelatedPublishers)
          {
              e.RelatedPublishers.Add(rp);
          }
          foreach (Download d in pResult._Downloads)
          {
              e.Downloads.Add(d);
          }
          foreach (PublisherDomain pd in pResult._PublisherDomains)
          {
              e.PublisherDomains.Add(pd);
          }
          e.SaveChanges();
      }
  }
  </code></pre>
  
  <p>The rest of the tables (RelatedPublishers, Downloads and PublisherDomains) are empty, because they have non of the mentioned objects in the first <code>pResult</code>, with Primary Key - Id which is the defined entity key.</p>
  
  <p>The first <code>pResult</code> is skipped because it exists, and the second one throws the exception on the PK violation.</p>
  
  <p>Could it be that I'm trying to insert the 2nd <code>pResult</code> in the first row (<code>id=0</code> on Publishers)? and if not, then what am I doing wrong?</p>