After reading this article I decided to take a closer look at the way I was using Dapper.
I ran this code on an empty database
var members = new List
Using the Execute
method with only one insert statement will never do a bulk insert or be efficient. Even the accepted answer with a Transaction
doesn't do a Bulk Insert
.
If you want to perform a Bulk Insert
, use the SqlBulkCopy
https://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlbulkcopy
You will not find anything faster than this.
Disclaimer: I'm the owner of the project Dapper Plus
This project is not free but offers all bulk operations:
(Use under the hood SqlBulkCopy
)
And some more options such as outputting identity values:
// CONFIGURE & MAP entity
DapperPlusManager.Entity()
.Table("Orders")
.Identity(x => x.ID);
// CHAIN & SAVE entity
connection.BulkInsert(orders)
.AlsoInsert(order => order.Items);
.Include(x => x.ThenMerge(order => order.Invoice)
.AlsoMerge(invoice => invoice.Items))
.AlsoMerge(x => x.ShippingAddress);
Our library supports multiple providers: