0

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.

1 Answer 1

5

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);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.