Open dazinator opened 5 months ago
I have the same issue, what SQL version do you have? Mine is 12.0.2000.8 which is SQL Servier 14, I get "Cannot insert duplicate key ....." but I do not have duplicates, I have 35 000 items and I process them by 5000 at a time, on batch 3 or 4 it usually fails and shows some random item every time and says it is a duplicate
@Nazarii77 I don't think the implementation of BulkSaveChangesAsync
is complete due to this issue.
I ended up ditching it and using the BulkInsert method instead.
I am using
await Context.BulkSaveChangesAsync(bulkConfig, cancellationToken: cancellationToken);
This is called within a surrounding transaction which is created like so:
There are 9k entities that have been added to the DBContext and these are all successfully inserted and the transaction completes successfully.
However if I then inspect the DbContext change tracker - all 9k entities are still in an Added() state. This means if something else repeats a call to
DbContext.SaveChanges
within the same scope, it tries to insert the entities and fails as now duplicate keys.Ideally,
BulkSaveChangesAsync
should update the entity state to reflect the new state?