Entity Framework - Bulk Extensions



What is Entity Framework Bulk Extensions?

Bulk Extensions are advanced methods offered by the Entity Framework Extensions third-party library created and maintained by ZZZ Projects. These methods, though not free, significantly boost performance for operations such as inserting, updating, or deleting entities in your database.

Key Points

  • Cost − The library is a paid solution, offering advanced features not available in the standard Entity Framework.
  • Ease of Use − Methods are extremely easy to use; for instance, you can replace SaveChanges with BulkSaveChanges.
  • Performance − These methods can reduce save times by up to 98% (50x faster) compared to SaveChanges.
  • Flexibility − The library offers hundreds of options to handle any scenario.

To see how easy it is to implement this library, consider the following code snippet, where SaveChanges is replaced by BulkSaveChanges −

using (var context = new UniContext()) {
   context.AddRange(students);
   // context.SaveChanges();  // Standard EF method
   context.BulkSaveChanges();  // Bulk extension method
}

Why Use Entity Framework Extensions?

Using the standard Entity Framework involves a database roundtrip for each entity being saved. This is not noticeable with a small number of entities but can significantly degrade performance with hundreds or thousands of entities.

Entity Framework Extensions optimize these operations by minimizing database roundtrips. Techniques such as SqlBulkCopy and the use of temporary tables under the hood help improve efficiency and speed.

In addition to being fast, the library offers over 100+ options to allow you to save your entities the way you want. Some noticeable options include:

  • InsertIfNotExists − Only insert entities that don't already exist in the database.
  • InsertKeepIdentity − Insert entities while keeping the identity values specified.
  • ColumnPrimaryKeyExpression − Perform an operation by choosing a different key than the one specified in the model.
  • ColumnInputExpression − Perform an operation by only inserting or updating specified properties. Other properties will be ignored.
  • IncludeGraph − Insert entities including all the graph associated with it.

Available Bulk Extension Methods

Entity Framework Extensions provide a variety of methods tailored to handle large volumes of data operations efficiently −

  • BulkSaveChanges − Replace the SaveChanges method to efficiently save entities tracked by the ChangeTracker.
  • BulkInsert − Allows the insertion of a large number of entities into the database.
  • BulkUpdate − Allows the updating of a large number of entities.
  • BulkDelete − Allows the deletion of a large number of entities.
  • BulkMerge − Acts as an "Add or Update" operation, allowing for the merging of a large number of entities into the database.
  • WhereBulkContains − Allows filtering a LINQ query with an unlimited amount of items. Supports using a surrogate key.

The library offers many more paid and free methods.

Conclusion

While the use of a third-party library like Entity Framework Extensions is entirely optional for a small number of entities, its value becomes increasingly apparent as the volume of entities to manage grows. For developers needing high performance in large-scale data operations, Entity Framework Extensions can provide significant efficiency gains.

Advertisements