Entity Framework Core Bulk Insert Example | Entity Framework Insert Multiple Records | Part-10

Published: 08 January 2022
on channel: Code with Salman
4,613
46

GITHUB LINK :https://github.com/Salmanmasood/ef-core
Any difference between calling SaveChanges() inside and outside a foreach loop?


If you call it inside the loop, EF will write back the changes to the database for every single entity (and every entity will be in its own, separate transaction).
The other way around, you'll make all your changes and EF will write them back all at once after the loop (in one single transaction for all entities together).

Is Entity Framework Add Really Slow?
In fact, the Add method is not slow at all. Adding an entity to a list cannot be that slow. It's the DetectChanges method invoked inside the Add method which is insanely slow!
Using the Add method in a loop is usually a poor practice which impacts your application performance severely when poorly used.
USE AddRange over Add (Recommended)
SET AutoDetectChanges to false
SPLIT SaveChanges in multiple batches


On this page of the site you can watch the video online Entity Framework Core Bulk Insert Example | Entity Framework Insert Multiple Records | Part-10 with a duration of hours minute second in good quality, which was uploaded by the user Code with Salman 08 January 2022, share the link with friends and acquaintances, this video has already been watched 4,613 times on youtube and it was liked by 46 viewers. Enjoy your viewing!