In this Video, I am going to discuss Bulk Insert, Update, and Delete in Entity Framework. Please read our previous Video where we discussed Asynchronous Programming with Entity Framework. At the end of this Video, you will understand how to insert, update, and delete multiple entities in Entity Framework.

Note: We are going to work with the same example that we created in our Introduction to Entity Framework Database First Approach Video. Please read our introduction to Entity Framework Database First Video before proceeding to this Video.
Bulk Insert in Entity Framework:
Entity Framework 6 introduced DbSet.AddRange() method to add a collection of entities in one go. What basically the AddRange() method does is, it attaches a collection of entities to the context object with Added state and when we call the SaveChanges method, it will execute the INSERT SQL Command in the database for all the entities.
Bulk Insert Example:
In the following example, first, we create a student collection with three students, and these students we want to insert into the database with one go. Then we pass this student collection to the BulkInsert method and this method uses the AddRange method to insert all the students with one go. The AddRange method attaches the student entities to the context object with Added state and when we call the SaveChanges method, the student entities are added to the database.
Output:
Bulk Update in Entity Framework:
In the bulk update, first, we need to pull all the records which are needed to be updated and then update the properties one by one and finally call the SaveChanges() method to save all changes. The following example does the same. In the BulkUpdate method, first, it fetches all the records whose LastName is Taylor and then updates the FirstName and StandardId, once it updates the properties then it calls the SaveChanges method to save updated data into the database.
Output:
Bulk Delete in Entity Framework:
Entity Framework 6 introduced DbSet.RemoveRange() method to remove a collection of entities in one go. What basically the RemoveRange() method does is, it attaches a collection of entities to the context object with the Deleted state, and when we call the SaveChanges method, it will execute the DELETE SQL Command in the database for all the entities.
Output:
The examples we discussed in this Video are looks pretty and but the problem is, it will generate SQL insert, update, and delete script for each record and it is a known problem in Entity framework for bulk operation (Insert, Update or Delete) and at this time Microsoft does not have any recommended solution instead of using third party entity framework and that we will discuss in our next Video.
Note: EF Core improves the performance of AddRange and RemoveRange methods by executing the INSERT and DELETE commands for all entities in a single database round trip.