Today I was tackling with a problem. It was about inserting and updating 200000 records of data. I just wanted to find a solution rather than writing Store Procedure. I just searched on the web and found a library named Z.BulkOperation

I dare say I had never seen handling this amount of data nicely in application level without even using a line of SQL code, it's awesome!

 

Fire up visual studio and a simple console or MVC application and install the package via:

 

PM> Install-Package Z.BulkOperations

PM> Install-Package Z.EntityFramework.Extensions

 

And run the following code sample and see the result:

 

  1.    public ActionResult Insert()
  2.         {
  3.             DbContextClass contextClass = new DbContextClass();
  4.  
  5.             var userList = new List<User>();
  6.  
  7.             for (int i = 0; i < 500000; i++)
  8.             {
  9.                 userList.Add(new User
  10.                 {
  11.                     Email = "mails@ehsanghanbari.com",
  12.                     Id = Guid.NewGuid(),
  13.                     Password = "123",
  14.                     Username = "ehsan"
  15.                 });
  16.             }
  17.  
  18.             contextClass.BulkInsert(userList);
  19.  
  20.             return View();
  21.         }

 

The insert operation for 500000 of records would be 29.648 ms in my machine! And just for update operation for the same data:

 

  1.    public ActionResult Update()
  2.         {
  3.             DbContextClass contextClass = new DbContextClass();
  4.  
  5.             var userList = new List<User>();
  6.  
  7.             var users = contextClass.Users.ToList();
  8.  
  9.             foreach (var user in users)
  10.             {
  11.                 user.Email = "ehsan.ghanbari@htomail.com";               
  12.                 user.Password = "12343242";
  13.                 user.Username = "ehsanghanbari";
  14.  
  15.                 userList.Add(user);
  16.             }
  17.  
  18.             contextClass.BulkUpdate(userList);
  19.  
  20.             return View();
  21.         }

 

It resulted 28.232ms and for delete operation:

 

  1.         public ActionResult Delete()
  2.         {
  3.             DbContextClass contextClass = new DbContextClass();
  4.  
  5.             var userList = new List<User>();
  6.  
  7.             var users = contextClass.Users.ToList();
  8.  
  9.             contextClass.BulkDelete(users);
  10.  
  11.             return View();
  12.         }

 

It elapsed 14.324 ms.

 

I really enjoyed and hope you enjoy as well ;)


Tags: C# , Libraries