How to Increase EF Core Performance for Saving Multiple Entities?

Hello friends, I hope you all are doing great. In today's tutorial, we will have a look at How to Increase EF Core Performance for Saving Multiple Entities? EF Core works well for simple CRUD operations but for saving multiple entities EF Core provides poor performance. So, today we will use a third-party EF Core extension named "Z.EntityFramework.Extensions.EFCore", designed by ZZZ Projects, which will increase the EF Core performance significantly. I will be using a BulkSaveChanges Method of this library which is specifically designed for saving bulk data in an SQL database.

ZZZ Projects is a trusted company and has designed numerous .NET libraries, a few having downloads in millions. So, you can completely trust this extension library.

  • Here's a video for this tutorial, which will give you a practical demonstration of this project i.e. How to add this Library to your project and how to use this extension method BulkSaveChanges:

Before starting with the actual process, let's first have a look at What is EF Core?

What is EF Core?

EF Core is an Object Relational Mapper(ORM) and is used as a bridge between the ASP.Net Core application & its database. Thanks to EF Core, now there's no need to write complex SQL queries for database-related operations, instead, developers can easily perform all database CRUD operations in C# language(using EF Core). Although designed by Microsoft, but EF Core is a separate module and we can add it to our .NET application from the Nuget library.

Low Performance of EF Core

  • ORM(i.e. EF Core) provides a simple instructions-set for database CRUD operations and proves very friendly to developers, but it has few drawbacks as well.
  • One of the main disadvantages of EF Core is its low performance, which not only slows down your application but also increases the database interactions(cloud providers may charge extra).
  • Moreover, as the number of data(you want to save in the database) increases, the EF Core performance decreases.

EF Core Performance for multiple Entities

  • In a professional .NET application development, there come many scenarios where the developer needs to save multiple entities in the database i.e. user notifications, real-time messages etc.
  • When multiple entities are saved using EF Core, it doesn't save them in a single SQL session.
  • Instead, it takes multiple round-trips to the database, which increases the overall time for the data-saving process.

So, now let's have a look at How to increase the EF Core Performance for multiple entities by using BulkSaveChanges(provided by Z Extension), instead of SaveChanges(provided by EF Core):

What is BulkSaveChanges Method?

  • BulkSaveChanges is a simple function, automatically generated by EF Core Extension Library and is used for saving multiple entities in the database.
  • BulkSaveChanges increases the performance of EF Core by 3 to 4 times and its performance is exponential to a number of entities.
  • First of all, you need to download this project designed in ASP.NET Core:
Download Asp.Net Core Project
  • Open it in Visual Studio and first run the Migration commands for setting up the SQL database.

How to add EF Core Extension Library

  • If you check the NuGet packages in the above project, you will find these four packages installed in it, shown in the below figure:
  • You must be familiar with the first 3 NuGet packages, as they are used to add EF Core libraries in the .Net project.
  • The fourth one is the NuGet package of EF Core extension Library, designed by ZZZ Projects.
  • If you click on the Browse tab and make a search for "Z.EntityFramework", you will find results, as shown in the below figure:

Methods offered by Z Extension Library

  • After adding this NuGet package of ZZZ Projects, its methods will become readily available under the instance of DbContext class.
  • I am going to discuss BulkSaveChanges Method only(in today's lecture) but it has a wide range of methods for improving the performance of DBContext class, used in different scenarios, depending on requirements.
  • Here's a list of few other methods, offered by this EF Core Extension Library:

BulkSaveChanges Method to improve performance of EF Core

Now, let's have a look at the implementation of the BulkSaveChanges Method in our Asp.Net Core Application:
  • In the Models folder, open the SubjectRepository.cs file and here you will find two methods, named:
    • Add1() Method: It will use the default SaveChanges() Method of EFCore.
    • Add2() Method: It will be using the new BulkSaveChanges() Method of EF Core Extension Library.
  • Both of these methods are shown in the below figure:
  • As you can see in the above code, it's too easy to use the BulkSaveChanges() method, as it's called on the same instance of DBContext class, on which I have called the default SaveChanges() method.
  • Moreover, I have placed a stopwatch function around these methods to calculate the time taken by them, for saving 5000 entities in the database.
Now, let's save 5000 entities using these two methods, and look at the results:

SaveChanges vs BulkSaveChanges

  •  I have tested these methods three times each and created a comparison table.
  • In each of these testing trials, I have saved 5000 entities in the underlying SQL database.
  • Here's the result of the comparison, SaveChanges vs BulkSaveChanges: (all these readings are in milliseconds)
  • As you can see in the above comparison table, the BulkSaveChanges method is 3 to 4 times faster than the SaveChanges method.

For a practical demonstration of this project, watch the above video, where I have completely demonstrate How to add this Z extension Library to your project and how to use the BulkSaveChanges method to improve the performance of EF Core. So, that was all today, if you have any questions, please ask in the comments. Thanks for reading!!! :)

Syed Zain Nasir

I am Syed Zain Nasir, the founder of <a href=https://www.TheEngineeringProjects.com/>The Engineering Projects</a> (TEP). I am a programmer since 2009 before that I just search things, make small projects and now I am sharing my knowledge through this platform.I also work as a freelancer and did many projects related to programming and electrical circuitry. <a href=https://plus.google.com/+SyedZainNasir/>My Google Profile+</a>

Share
Published by
Syed Zain Nasir