Right now I loop through each entity and call ExecuteUpdateAsync
then after the loop I call CommitAsync
, but I’d like to know if there is a better way for updating ~500 records?
I see this bulk extensions library, but never used it and don’t know if it would help.
I’m using EF Core with a Code First approach, so I’d like to stay within the scope of EF Core.
Lastly, my data isn’t coming from my DB, it’s coming from a web service that’s being called to fetch the data.
// About 500 transfers, data doesn't come from DB
var transfers = new List<(string, decimal?, long, string, string)>();
using var transaction = await _dbContext.Database.BeginTransactionAsync();
try
{
foreach (var transfer in transfers)
{
var stripeTransfersResult = await _dbContext.StripeTransfers
.Where(p => p.TransferId == transfer.Item1)
.ExecuteUpdateAsync(setters => setters
.SetProperty(p => p.ExchangeRate, transfer.Item2)
.SetProperty(p => p.ExchangeAmount, transfer.Item3)
.SetProperty(p => p.ExchangeCurrency, transfer.Item4)
.SetProperty(p => p.StripePayoutId, transfer.Item5)
);
}
await transaction.CommitAsync(cancellationToken);
}
catch (DbException ex)
{
// handle exception
}