I'm having an algorithm which creates a huge set of data in lists which have to be inserted into a database table. I can just put a loop through the entity save changes but is there a bit better way than that.
CodePudding user response:
You can use AddRange
for insert multiple rows into table like this:
var list=new List<YourModel>();
await dbContext.YouModels.AddRangeAsync(list);
await dbContext.SaveChangeAsync();
That works fine but it's recommended to use an extension for EF called EFCore.BulkExtensions
with high performance like this:
var list=new List<YourModel>();
await dbContext.BulkInsertAsync(entities);