Entity Framework throws "An item with the same key has already been added"

c# entity-framework entity-framework-core mariadb

Question

I'm getting this exception:

An item with the same key has already been added. Key: p2072

This is when SaveChanges is called after adding the entities to the DbContext.

The strange thing is that it says "Key: p2072", but it doesn't match any key of my entity.

The primary key is configured as follows:

modelBuilder.Entity<RequestEntity>().HasKey(e => new { e.Nif, e.Especialidad, e.Cuerpo });

where:

  • Nif is string
  • Especialidad is int
  • Cuerpo is Enum

You can see the complete definition of the entity. It's as follows:

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; }
}

Provicias, Cuerpo, Idiomas and Estado are Enums. Notice that there aren't any relationships to other entities, only primitive types.

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.

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.

Edit

This is the stack trace:

at System.ThrowHelper.ThrowAddingDuplicateWithKeyArgumentException(Object key) at System.Collections.Generic.Dictionary2.TryInsert(TKey key, TValue value, InsertionBehavior behavior) at System.Collections.Generic.Dictionary2.Add(TKey key, TValue value)
at Microsoft.EntityFrameworkCore.Update.ReaderModificationCommandBatch.CreateStoreCommand() at Microsoft.EntityFrameworkCore.Update.ReaderModificationCommandBatch.Execute(IRelationalConnection connection) at Microsoft.EntityFrameworkCore.Update.Internal.MySqlBatchExecutor.Execute(IEnumerable1 commandBatches, IRelationalConnection connection) at Microsoft.EntityFrameworkCore.Storage.RelationalDatabase.SaveChanges(IReadOnlyList1 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)

Edit 2

Just before the SaveChanges, I've put this line:

        var duplicates = requestEntities.GroupBy(e=>new{e.Nif, e.Especialidad, e.Cuerpo}).Select(x=>new{x.Key, Count = x.Count()})
            .Where(x => x.Count > 1)
            .ToList();

The funny thing is that duplicates has nothing (0 elements). What's going on?

1
1
1/15/2019 10:27:50 PM

Popular Answer

(Posted on behalf of the question author).

It happens to be a bug in the Pomelo EF MySQL provider I'm using! Updating to the latest version solves the problem.

0
1/15/2019 10:26:46 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow