Home > database >  Insert multiple rows into the database using Entity Framework in ASP.NET Web API
Insert multiple rows into the database using Entity Framework in ASP.NET Web API

Time:05-25

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);
  • Related