Updated February 14, 2023
Introduction to Entity Framework Extensions
Entity Framework Extensions is nothing but a library that dramatically progresses the Entity Framework performances by using bulk and batch operations. Most people get beneficiaries through Extension, so they often use the library to report performance improvement more than 50x times. We can get the library through NuGet Extension methods which are included automatically in the DbContext.
What are Entity Framework Extensions?
Entity Framework Extension is a library that progresses EF performances using batch and bulk operations. Entity Framework is supposed to be a slow process while storing multiple entities. The performance issues are caused by the DeleteChanges method and the number of database round-trips. EF Extension helps improve performance by only a few round trips.
Entity Framework Extensions Bulk Operations
To improve the Entity Framework performance, we use the EF Extensions to approach the Bulk SaveChanges and Bulk Operations. To solve the EF performance issues by storing with bulk operations and lots of flexible aspects, let’s see the following features available if EF Extensions.
- BulkSaveChanges
- BulkMerge
- BulkInsert
- BulkDelete
- BulkUpdate
- UpdateFromQuery
- DelateFromQuery
Let’s see the following code, which describes the features:
Code:
var dbContext = new CustomerContext();
// here the db_Context code..
dbContext.BulkSaveChanges();// to make it easy just use of the BikSaveChanges method
// to make a easy customize
dbContext.BulkSaveChanges(operation => operation.BatchSize = 1000);
// bulk- operations
dbContext.BulkDelete(products);
dbContext.BulkInsert(products);
dbContext.BulkUpdate(products);
// to customize the primary key
dbContext.BulkMerge(products, operation => {
operation.ColumnPrimaryKeyExpression = customer => customer.Code;
});
Here explains the scalability of SQL Server Benchmarks as follows:
Operations | 100 – Rows | 1,000 – Rows | 10,000 – Rows |
BulkSaveChanges | 20 ms | 200 ms“ | 2,000 ms |
BulkInsert | 2 ms | 6 ms | 25 ms |
BulkUpdate | 27 ms | 50 ms | 80 ms |
BulkDelete | 25 ms | 45 ms | 70 ms |
BulkMerge | 30 ms | 65 ms | 160 ms |
To do the BulkSaveChanges method, we have to store the hundreds and thousands of entities, but it will not be done through Entity Framework but through Entity Framework performance. So BulkSaveChanges, like the SaveChanges, performs in the best way faster. So upgrade the SaveChanges method by adding “bulk” as BulkSaveChanges.
Code:
var db_context = new ProductContext();
// here the db_context code
db_context.BulkSaveChanges();
db_context.BulkSaveChanges(operation => operation.BatchSize = 1000);
See the scalability of BulkSaveChanges – which is faster than SaveChanges with an entity and becomes 10-50x times faster with hundreds and thousands of entities. The BulkSaveChanges method is the updated version of SaveChanges.
BulkSaveChanges Method
The BulkSaveChanges method is the upgraded version of SaveChanges. The entire changes are done in context, persisted in the database, and reduced the number of round-trips made in a database.
Let’s see the supported data of BulkSaveChanges as follows:
- Navigation Property
- Association (One to One, One to Many, Many to Many, and so on)
- Inheritance (TPT, TPC, TPH)
- Complex Type
- Self-Hierarchy
- Enum
Example:
Code:
db_context.Products.AddRange(listToAdd); // to include
db_context.Products.RemoveRange(listToRemove); // to delete
listToModify.ForEach(x => x.DateModified = DateTime.Now); // to update
// simple to apply
db_context.BulkSaveChanges();
// customization is easy
db_context.BulkSaveChanges(bulk => bulk.BatchSize = 100);
Just look at the Performance Comparator:
Operations | 1,000 Entities | 2,000 Entities | 5,000 Entities |
BulkSaveChanges | 90 ms | 150 ms | 350 ms |
SaveChanges | 1000 ms | 2000 ms | 5000 ms |
Bulk Operation
To use Bulk Operations like Insert, Update, Delete, and Merging operate on a specific entity and pass the modification tracker to enhance the performance.
Code:
// to achieve particular bulk operations on entities
dbContext.BulkDelete(products);
dbContext.BulkInsert(products);
dbContext.BulkUpdate(products);
dbContext.BulkMerge(products);
To maintain the Bulk Operation directly, use the EF Model; if you want to change the column name or Inheritance (TPT, TPC, TPH), the Bulk Operation will maintain work as required.
1. Bulk Operation Methods
To overcome the SQLBulkCopy limits by including the high-performance bulk operation like bulk insert, delete, update, merge, and so on. In addition, the Bulk Operation gives enhanced features by enabling customization options like primary key and column and includes the child entity. Moreover, it will not use the ChangeTracker and DeleteChanges methods because they use BulkSaveChanges.
Let’s see the following Bulk Operations, which are available as follows:
- BulkInsert
- BulkUpdate
- BulkDelete
- BulkMerge
- BulkSynchronize
Let’s see one example of Bulk Operation as follows:
Code:
// simpler to use
dbcontext.BulkInsert(ListItems);
dbcontext.BulkUpdate(ListItems);
dbcontext.BulkDelete(ListItems);
dbcontext.BulkMerge(ListItems);
// customization is simple
dbcontext.BulkMerge(products, options =>
options.ColumnPrimaryKeyExpression = product => product.Code; });
To compare the performances:
Operations | 1,000 Entities | 2,000 Entities | 5,000 Entities |
SaveChanges | 1000 ms | 2000 ms | 5000 ms |
BulkInsert | 6 ms | 10 ms | 15 ms |
BulkUpdate | 50 ms | 55 ms | 65 ms |
BulkDelete | 45 ms | 50 ms | 60 ms |
BulkMerge | 65 ms | 80 ms | 110 ms |
2. Batch Operations
The Batch Operation Methods enable the DELETE or UPDATE Operation achievement directly in the db using the queries in LINQ without adding the entities into the context.
The entire thing is executed on the database side and gives the top performance available; there are two types of batch operations available they are:
- DeleteFromQuery
- UpdateFromQuery
Examples of Batch Operations:
Code:
// to delete the employee details which are inactive for 2 years
dbcontext.Employees
.Where(x => x.LastLogin < DateTime.Now.AddYears(-2))
.DeleteFromQuery();
// to update the employees details which inactive for 2 years
dbcontext.Employees
.Where(x => x.IsActive && x.LastLogin < DateTime.Now.AddYears(-2))
.UpdateFromQuery(x => new Employee {IsActive = false});
Let’s see the Performance Comparison:
Operations | 1,000 Entities | 2,000 Entities | 5,000 Entities |
SaveChanges | 1000 ms | 2000 ms | 5000 ms |
DeleteFromQuery | 1 ms | 1 ms | 1 ms |
UpdateFromQuery | 1 ms | 1 ms | 1 ms |
Entity Framework Extensions Method
They are extending the DbContext with enhanced performance by using the batch and bulk operations and BulkSaveChanges while executing the Bulk-Operations stores many entities to maximize performance. In addition, BatchSaveChanges combines the SQL generated by SaveChanges to minimize the database round-trip.
Example of Entity Framework Extensions
Different examples are mentioned below:
Let’s see the sample program, a StartUp class, to initialize the database and build the 1000 units in the database.
Code:
using System;
using System.Linq;
namespace Sample_Program
{
class StartUp
{
static void Main()
{
ProductEntities dbContext = new ProductEntities();
for (int i = 0; i < 1000; i++)
{
Vehicle carObj = new Vehicle();
carObj.Model = carObj.GetModel(i);
carObj.Origin = carObj.GetOrigin(i);
carObj.Price = carObj.GetPrice(i);
carObj.Year = carObj.GetYear(i);
dbContext.Cars.Add(carObj);
}
dbContext.SaveChanges();
//in the form on LINQ Method
Console.WriteLine("LINQ Method\n");
var queryFerrari =
from c in dbContext.Cars
where c.Model.Contains("Ferrari")
orderby c.Price
select c;
foreach (Vehicle my_Car in q_Ferrari)
{
Console.WriteLine(my_Car);
}
//Extension method
Console.WriteLine("\n\nExtentsion Method:");
var queryBulgaria = dbContext.Cars
.Where(c => c.Origin.Contains("Bulgaria"))
.OrderBy(c => c.Price);
foreach (Vehicle my_Car in q_Bulgaria)
{
Console.WriteLine(my_Car);
}
}
}
}
Output:
Conclusion
In this article, we have seen the EF Extension, which contains various methods to increase the database’s performance and reduce the unwanted round-trip of the database. It includes operations like BulkSaveChanges and Batch Operations, which helps enhance the database performance.
Recommended Articles
This is a guide to Entity Framework Extensions. Here we discuss the introduction, entity framework extensions, bulk operations, and examples. You may also have a look at the following articles to learn more –