Bulk inserts taking longer than expected using Dapper

后端 未结 6 613
难免孤独
难免孤独 2020-11-29 18:40

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         


        
6条回答
  •  有刺的猬
    2020-11-29 18:45

    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.

    Dapper Plus

    Disclaimer: I'm the owner of the project Dapper Plus

    This project is not free but offers all bulk operations:

    • BulkInsert
    • BulkUpdate
    • BulkDelete
    • BulkMerge

    (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:

    • SQL Server
    • SQL Compact
    • Oracle
    • MySql
    • PostgreSQL
    • SQLite
    • Firebird

提交回复
热议问题