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:
public ActionResult Insert() { DbContextClass contextClass = new DbContextClass(); var userList = new List<User>(); for (int i = 0; i < 500000; i++) { userList.Add(new User { Email = "mails@ehsanghanbari.com", Id = Guid.NewGuid(), Password = "123", Username = "ehsan" }); } contextClass.BulkInsert(userList); return View(); }
The insert operation for 500000 of records would be 29.648 ms in my machine! And just for update operation for the same data:
public ActionResult Update() { DbContextClass contextClass = new DbContextClass(); var userList = new List<User>(); var users = contextClass.Users.ToList(); foreach (var user in users) { user.Email = "ehsan.ghanbari@htomail.com"; user.Password = "12343242"; user.Username = "ehsanghanbari"; userList.Add(user); } contextClass.BulkUpdate(userList); return View(); }
It resulted in 28.232ms and for delete operation:
public ActionResult Delete() { DbContextClass contextClass = new DbContextClass(); var userList = new List<User>(); var users = contextClass.Users.ToList(); contextClass.BulkDelete(users); return View(); }
It elapsed 14.324 ms.
I really enjoyed and hope you enjoy as well ;)
Category: Software
Tags: Entity Framework Tools Performance