UpsertRange inserting duplicates in MySql
See original GitHub issueHey all, I’m having an issue with UpsertRange inserting duplicates and I can’t work out why, possibly a bug?
I have the following trying to insert a list of 1000 POCO entity classes
IEnumerable<Entry> entries = transformedEntryModels.Select(Map);
await _entriesContext.UpsertRange(entry)
.On(entity => new { entity.ExternalId, entity.ProviderType })
.RunAsync();
I run the same batch (with the same ExternalId & ProviderType), and I get duplicate rows for all of them… I would hope that it would try and run an update against all of those rows (and effectively do nothing since the data is identical)
The POCO entity class is crazy simple, and we’re using EF Core & MySql The ID is the primary key (By convention) as per the MS doco --> https://docs.microsoft.com/en-us/ef/core/modeling/keys
public class Entry
{
public long ID { get; set; }
public string AgentCode { get; set; }
public string ExternalAgentId { get; set; }
public ProviderType ProviderType { get; set; }
public DateTimeOffset DateTimeUtc { get; set; }
public int? Rating { get; set; }
public string ExternalId { get; set; }
public string Title { get; set; }
public string Text { get; set; }
public string Location { get; set; }
public string State { get; set; }
public string PostalCode { get; set; }
public string City { get; set; }
public string Neighbourhood { get; set; }
public string Name { get; set; }
public string OriginalSource { get; set; }
public DateTimeOffset? Date { get; set; }
public string S3Key { get; set; }
}
We have no additional constraints set on DbContext via fluent api or anything like that…
If it helps (to see the versions/dependencies) here is the .csporoj
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<TargetLatestRuntimePatch>true</TargetLatestRuntimePatch>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
<WarningsAsErrors></WarningsAsErrors>
<TreatWarningsAsErrors>false</TreatWarningsAsErrors>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="AWSSDK.S3" Version="3.3.104.34" />
<PackageReference Include="AsyncEnumerator" Version="3.1.0" />
<PackageReference Include="AWSSDK.S3" Version="3.3.104.36" />
<PackageReference Include="FlexLabs.EntityFrameworkCore.Upsert" Version="3.0.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="2.2.6" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Relational" Version="2.2.6" />
<PackageReference Include="Microsoft.Extensions.Logging" Version="3.0.0" />
<PackageReference Include="MySql.Data.EntityFrameworkCore" Version="8.0.18" />
<PackageReference Include="Newtonsoft.Json" Version="12.0.2" />
</ItemGroup>
</Project>
Thanks for your help, I’m currently looking for alternative solutions :\
Issue Analytics
- State:
- Created 4 years ago
- Comments:7 (3 by maintainers)
Top GitHub Comments
Ok for future reference, adding an HasAlternateKey via fluent API in DbContext fixed my issue
modelBuilder.Entity<Review>().HasAlternateKey(r => new { r.ExternalId, r.ProviderType });
Ahh… seems like you’re using MySql…
The thing about MySql… there’s no way to specify which columns you’re doing the upsert on in MySql - it will use the unique index that is set up on the table to identify matches…
All you have to do is create a unique index on your table with your keys. I’m not sure if you’re allowed to have other unique indexes on the same table for this to work, but that’s just a limitation of MySql syntax