Bug #95528 AddRange throws "Duplicate entry 'x' for key 'PRIMARY'"
Submitted: 24 May 2019 15:52 Modified: 29 Jun 2019 13:03
Reporter: Mike Alhayek Email Updates:
Status: No Feedback Impact on me:
None 
Category:Connector / NET Severity:S1 (Critical)
Version:8.0.16 OS:Windows
Assigned to: CPU Architecture:Any

[24 May 2019 15:52] Mike Alhayek
Description:
I am trying to use MySQL connector with an ASP.NET Core 2.2 based project.

However, when I try to insert multiple records using `.AddRange()` on the repository I get the following error 

> Duplicate entry 'x' for key 'PRIMARY'

How to repeat:
Here is my table

```
CREATE TABLE `realestate_property_fields` (
   `Id` int(11) unsigned NOT NULL AUTO_INCREMENT,
   `SystemName` varchar(255) NOT NULL,
   `StandardName` varchar(255) DEFAULT NULL,
   `LongName` varchar(255) NOT NULL,
   `DbName` varchar(255) DEFAULT NULL,
   `ShortName` varchar(255) DEFAULT NULL,
   `MaximumLength` int(11) DEFAULT NULL,
   `DataType` varchar(50) DEFAULT NULL,
   `Precision` int(11) DEFAULT NULL,
   `Searchable` tinyint(1) DEFAULT '0',
   `Interpretation` varchar(50) DEFAULT NULL,
   `Alignment` varchar(50) DEFAULT NULL,
   `UseSeparator` tinyint(1) DEFAULT NULL,
   `EditMaskId` varchar(50) DEFAULT NULL,
   `LookupName` varchar(50) DEFAULT NULL,
   `MaxSelect` int(11) DEFAULT NULL,
   `Units` varchar(255) DEFAULT NULL,
   `Index` tinyint(1) DEFAULT NULL,
   `Minimum` decimal(30,4) DEFAULT NULL,
   `Maximum` decimal(30,4) DEFAULT NULL,
   `Default` tinyint(1) DEFAULT NULL,
   `Required` tinyint(1) DEFAULT '0',
   `SearchHelpId` varchar(50) DEFAULT NULL,
   `Unique` tinyint(1) DEFAULT '0',
   `MetadataEntryId` varchar(100) NOT NULL,
   `ModTimeStamp` tinyint(1) DEFAULT NULL,
   `ForeignKeyName` varchar(50) DEFAULT NULL,
   `ForeignField` varchar(50) DEFAULT NULL,
   `InKeyIndex` tinyint(1) DEFAULT '0',
   `IntegrationId` int(11) NOT NULL,
   `ResourceName` varchar(50) DEFAULT NULL,
   `ClassName` varchar(50) DEFAULT NULL,
   `DataTypeFormatId` int(11) DEFAULT NULL,
   `FieldDisplayType` varchar(50) NOT NULL DEFAULT 'Always',
   PRIMARY KEY (`Id`)
 ) ENGINE=InnoDB AUTO_INCREMENT=56 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci
```

Here is a stripped down version of my DbContext

```
public class ApplicationDbContext : IdentityDbContext<User, Role, int>
{
    public virtual DbSet<Field> Fields { get; set; }

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        base.OnModelCreating(modelBuilder);

        foreach (IMutableEntityType entityType in modelBuilder.Model.GetEntityTypes())
        {
            var table = entityType.Relational().TableName;
            if (table.StartsWith("AspNet"))
            {
                entityType.Relational().TableName = Str.StudlyToSnake(table.Substring(6));
            }

            foreach (IMutableProperty property in entityType.GetProperties())
            {
                if (property.PropertyInfo == null)
                {
                    continue;
                }

                if (property.IsPrimaryKey() && Helper.IsPrimaryKey(property.PropertyInfo))
                {
                    // At this point we know that the property is a primary key
                    modelBuilder.Entity(entityType.ClrType)
                                .Property(property.Name)
                                .ValueGeneratedOnAdd()
                                .Metadata.BeforeSaveBehavior = PropertySaveBehavior.Ignore;
                }
                else if (property.PropertyInfo.PropertyType.IsBoolean())
                {
                    modelBuilder.Entity(entityType.ClrType)
                                .Property(property.Name)
                                .HasConversion(new BoolToZeroOneConverter<short>());
                }
                else if (property.PropertyInfo.PropertyType.IsTrueEnum())
                {
                    // At this point we know that the property is an enum.
                    // Add the EnumToStringConverter converter to the property so that
                    // the value is stored in the database as a string instead of number 

                    modelBuilder.Entity(entityType.ClrType)
                                .Property(property.Name)
                                .HasConversion<string>();
                }
            }
        };
    }
}
```

Then from my code, I do the following
var newFields = new List<Field>{
   new Field(){
        Name = "some name"
   }
};

ApplicationDbContext.Fields.AddRange(newFields );
ApplicationDbContext.SaveChanges();
[29 May 2019 13:03] MySQL Verification Team
Thank you for the bug report. Are you able to provide a complete code test file?. Thanks.
[30 Jun 2019 1:00] Bugs System
No feedback was provided for this bug for over a month, so it is
being suspended automatically. If you are able to provide the
information that was originally requested, please do so and change
the status of the bug back to "Open".