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();