In my previews blog post I wrote about Z.bulkOperation. I migrated to sqlbulktools just because it's free with the same ability! (to be frank). Now to see the reason for my migration, fire up visual studio and Install the latest version of the library using the following command in console package manager:
static void Main(string[] args) { var list = new List<User>(); for (int i = 0; i < 100000; i++) { list.Add(new User { Id = Guid.NewGuid(), Name = "Ehsan", Email = "mail@ehsanghanbari.com" }); } Insert(list); Update(list); Delete(list); } public static void Insert(IList<User> users ) { var bulkOperation = new BulkOperations(); using (var trans = new TransactionScope()) { using (var connection = new SqlConnection(ConfigurationManager.ConnectionStrings["MyContext"].ConnectionString)) { bulkOperation.Setup<User>() .ForCollection(users) .WithTable("Users") .AddAllColumns() .BulkInsert() // .SetIdentityColumn(x => x.Id, ColumnDirection.Output) .Commit(connection); } trans.Complete(); } } public static void Update(IList<User> users) { var bulkOperation = new BulkOperations(); using (var trans = new TransactionScope()) { using (var conn = new SqlConnection(ConfigurationManager.ConnectionStrings["MyContext"].ConnectionString)) { bulkOperation.Setup<User>() .ForCollection(users) .WithTable("Users") .AddAllColumns() .BulkUpdate() .MatchTargetOn(x => x.Id) .Commit(conn); } trans.Complete(); } } public static void Delete(List<User> list) { var bulkOperation = new BulkOperations(); using (var trans = new TransactionScope()) { using (var conn = new SqlConnection(ConfigurationManager.ConnectionStrings["MyContext"].ConnectionString)) { bulkOperation.Setup<User>() .ForCollection(list) .WithTable("Users") .AddColumn(x => x.Id) .BulkDelete() .MatchTargetOn(x => x.Id) .Commit(conn); } trans.Complete(); } }
Install-Package sqlbulktools
And get started!
Simply copy and paste the above code in your console application and run it. The result of CRUD operation with sqlbulktools on 100000 of records in my machine would be:
insert 5,066ms
update 12,372ms
delete 2,0305ms
It could be different in every machine. But I think it's a good idea to use it in an enterprise application. Cheers!
Category: Software
Tags: Entity Framework Tools Performance