DEV Community

Nguyen Dinh Nghia
Nguyen Dinh Nghia

Posted on

Bulk Insert, Bulk Delete, Bulk Update with Dapper by Bulk.Dapper

After researching Dapper, I discovered that it does not support bulk insert, bulk update, or bulk delete. Therefore, I have written a library to support these operations.

Performance

The table below describes the performance in milliseconds of the methods corresponding to the respective database types with 500 records.

Method SQL Server MySQL PosgreSQL SQLite
Insert 24900 8600 5300 7900
Bulk Insert 111 88 53 89
Update 9500 22600 4800 6500
Bulk Update 889 112 96 N/A
Delete 17200 23800 5400 6100
Bulk Delete 194 150 1200 115

Bulk.Daper performance
SQLite does not meet the bulk update requirement because it does not support any bulk update method.

Nuget

dotnet add package Bulk.Dapper
Enter fullscreen mode Exit fullscreen mode

Example

for example User

   public class User
    {
        [Key]
        public int id { get; set; }
        public string name { get; set; }
        public int age { get; set; }
    }

    List<User> users = new List<User>
    {
            new User { name = "Joe", age = 10 },
            new User { name = "Donal", age = 10 }
    };
Enter fullscreen mode Exit fullscreen mode

Insert

Normal

       connection.BulkInsert(users);
Enter fullscreen mode Exit fullscreen mode

Async

       connection.BulkInsertAsync(users);
Enter fullscreen mode Exit fullscreen mode

Update

Normal

       connection.BulkUpdate(users);
Enter fullscreen mode Exit fullscreen mode

Async

       connection.BulkUpdateAsync(users);
Enter fullscreen mode Exit fullscreen mode

Delete

       connection.BulkDelete(users);
Enter fullscreen mode Exit fullscreen mode

Async

       connection.BulkDeleteAsync(users);
Enter fullscreen mode Exit fullscreen mode

Set Table name and column name

Can change the table name by attribute Table, column name by attribute ColumnName

    [Table("Users")]
    public class UserChangeColumn
    {
        [Key]
        public int id { get; set; }

        [ColumnName("name")]
        public string fullname { get; set; }
        public int age { get; set; }
    }
Enter fullscreen mode Exit fullscreen mode

Note

Other attributes are the same as Dapper.Contrib

Source code

Top comments (0)