I have used Entity Framework to insert data into SQL tables.
For larger number of records, instead of Add(), I have used AddRange() and called SaveChanges() later.
It's still taking too much time to insert records - are there any solutions to increase the speed?
_Repository.InsertMultiple(deviceDataList);
await _Repository.SaveAsync();
public void InsertMultiple(List<string> deviceDataList)
{
context.Devices.AddRange(devices);
}
Using AddRange over Add is already a great improvement. It fixes the part that's slow in the Application.
However, the SaveChanges still take a lot of time because one database round-trip is made for every entity you save. So if you have 10k entities to insert, 10,000 database round-trip will be made which is INSANELY slow.
Disclaimer: I'm the owner of Entity Framework Extensions
This library is not free but allows you to perform all bulk operations including BulkSaveChanges and BulkInsert:
Example
// Easy to use
context.BulkSaveChanges();
// Easy to customize
context.BulkSaveChanges(bulk => bulk.BatchSize = 100);
// Perform Bulk Operations
context.BulkDelete(customers);
context.BulkInsert(customers);
context.BulkUpdate(customers);
// Customize Bulk Operations
context.BulkInsert(customers, options => {
options => options.IncludeGraph = true;
});
context.BulkMerge(customers, options => {
options.ColumnPrimaryKeyExpression =
customer => customer.Code;
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With