You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This is called within a surrounding transaction which is created like so:
public async Task ExecuteWithTransaction(Func<Task> action)
{
var executionStrategy = Context.Database.CreateExecutionStrategy();
await executionStrategy.ExecuteAsync(async () =>
{
await using var transaction = await Context.Database.BeginTransactionAsync();
try
{
await action(); // it's called here
await transaction.CommitAsync();
}
catch (Exception)
{
await transaction.RollbackAsync();
throw;
}
});
}
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?
The text was updated successfully, but these errors were encountered:
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
I ran into the same problem. One possible solution is to attach the entities to let EF know the entities already exist in the database. But yeah I agree, this method is buggy.
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?The text was updated successfully, but these errors were encountered: