question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

UpsertRange inserting duplicates in MySql

See original GitHub issue

Hey 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:closed
  • Created 4 years ago
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
Voochichichicommented, Oct 16, 2019

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

1reaction
artiomchicommented, Oct 15, 2019

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

Read more comments on GitHub >

github_iconTop Results From Across the Web

UpsertRange() MySQL Extended inserts · Issue #63
Some rows are duplicated there, and just latest record is stored. MySQL have CSV importer, but i can't use that because of this....
Read more >
ON DUPLICATE KEY UPDATE to upsert and modify data in ...
The `INSERT...ON DUPLICATE KEY UPDATE` clause lets you handle scenarios where a record should be modified if it exists and added if it...
Read more >
Ignore duplicate key insert with Entity Framework
You can do this: var newUserIDs = NewUsers.Select(u => u.UserId).Distinct().ToArray(); var usersInDb = dbcontext.Users.
Read more >
13.2.7.2 INSERT ... ON DUPLICATE KEY UPDATE Statement
If you specify an ON DUPLICATE KEY UPDATE clause and a row to be inserted would cause a duplicate value in a UNIQUE...
Read more >
How to prevent duplicate INSERT in MySQL
Note − Use the INSERT IGNORE command rather than the INSERT command. If a record doesn't duplicate an existing record, then MySQL inserts...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found