title: Entity Framework throws "An item with the same key has already been added"
body: <p>I'm getting this exception:</p>
  
  <blockquote>
    <p>An item with the same key has already been added. Key: p2072</p>
  </blockquote>
  
  <p>This is when <code>SaveChanges</code> is called after adding the entities to the <code>DbContext</code>.</p>
  
  <p>The strange thing is that it says <strong>"Key: p2072"</strong>, but it doesn't match any key of my entity.</p>
  
  <p>The primary key is configured as follows:</p>
  
  <pre><code>modelBuilder.Entity&lt;RequestEntity&gt;().HasKey(e =&gt; new { e.Nif, e.Especialidad, e.Cuerpo });
  </code></pre>
  
  <p>where:</p>
  
  <ul>
  <li>Nif is <code>string</code></li>
  <li>Especialidad is <code>int</code></li>
  <li>Cuerpo is <code>Enum</code></li>
  </ul>
  
  <p>You can see the complete definition of the entity. It's as follows:</p>
  
  <pre><code>public class RequestEntity
  {
      public string Nif { get; set; }
      public Provincias Provincias { get; set; }
      public Cuerpo Cuerpo { get; set; }
      public int Especialidad { get; set; }
      public Estado Estado { get; set; }
      public Idiomas Idiomas { get; set; }
      public int Orden { get; set; }
  }
  </code></pre>
  
  <p><strong>Provicias, Cuerpo, Idiomas and Estado are Enums</strong>. Notice that there aren't any relationships to other entities, only primitive types.</p>
  
  <p>I'm having a hard time debugging this, because there are +23,000 entities to be added and it seems that there aren't any duplicates.</p>
  
  <p>Moreover, the key that it's showing (p2072) has no sense. The only string in the key is  Nif, but there is no matching Nif.</p>
  
  <h2>Edit</h2>
  
  <p>This is the stack trace:</p>
  
  <blockquote>
    <p>at
    System.ThrowHelper.ThrowAddingDuplicateWithKeyArgumentException(Object
    key)    at System.Collections.Generic.Dictionary<code>2.TryInsert(TKey key,
    TValue value, InsertionBehavior behavior)    at
    System.Collections.Generic.Dictionary</code>2.Add(TKey key, TValue value)<br>
    at
    Microsoft.EntityFrameworkCore.Update.ReaderModificationCommandBatch.CreateStoreCommand()
    at
    Microsoft.EntityFrameworkCore.Update.ReaderModificationCommandBatch.Execute(IRelationalConnection
    connection)    at
    Microsoft.EntityFrameworkCore.Update.Internal.MySqlBatchExecutor.Execute(IEnumerable<code>1
    commandBatches, IRelationalConnection connection)    at
    Microsoft.EntityFrameworkCore.Storage.RelationalDatabase.SaveChanges(IReadOnlyList</code>1
    entries)    at
    Microsoft.EntityFrameworkCore.ChangeTracking.Internal.StateManager.SaveChanges(IReadOnlyList`1
    entriesToSave)    at
    Microsoft.EntityFrameworkCore.ChangeTracking.Internal.StateManager.SaveChanges(Boolean
    acceptAllChangesOnSuccess)    at
    Microsoft.EntityFrameworkCore.DbContext.SaveChanges(Boolean
    acceptAllChangesOnSuccess)    at
    Microsoft.EntityFrameworkCore.DbContext.SaveChanges()    at
    Plugin.Clm.Importer.Importer.SaveNewResults(ImportResult results)</p>
  </blockquote>
  
  <h2>Edit 2</h2>
  
  <p>Just before the <code>SaveChanges</code>, I've put this line:</p>
  
  <pre><code>        var duplicates = requestEntities.GroupBy(e=&gt;new{e.Nif, e.Especialidad, e.Cuerpo}).Select(x=&gt;new{x.Key, Count = x.Count()})
              .Where(x =&gt; x.Count &gt; 1)
              .ToList();
  </code></pre>
  
  <p>The funny thing is that <code>duplicates</code> has nothing (0 elements). What's going on?</p>