Dapper Plus - Bulk Insert

Description

INSERT entities using the Bulk Operation.

Example - Insert Single

INSERT a single entity with Bulk Operation.

  1. DapperPlusManager.Entity<Customer>().Table("Customers");
  2. using (var connection = new SqlConnection(FiddleHelper.GetConnectionStringSqlServerW3Schools()))
  3. {
  4. connection.BulkInsert(new List<Customer>() { new Customer() { CustomerName = "ExampleBulkInsert", ContactName = "Example Name :" + 1}});
  5. }

Try it: .NET Core | .NET Framework

Example - Insert Many

INSERT many entities with Bulk Operation.

  1. DapperPlusManager.Entity<Customer>().Table("Customers");
  2. using (var connection = new SqlConnection(FiddleHelper.GetConnectionStringSqlServerW3Schools()))
  3. {
  4. connection.BulkInsert(customers);
  5. }

Try it: .NET Core | .NET Framework

Example - Insert with relation (One to One)

INSERT entities with a one to one relation with Bulk Operation.

  1. DapperPlusManager.Entity<Supplier>().Table("Suppliers").Identity(x => x.SupplierID);
  2. DapperPlusManager.Entity<Product>().Table("Products").Identity(x => x.ProductID);
  3. using (var connection = new SqlConnection(FiddleHelper.GetConnectionStringSqlServerW3Schools()))
  4. {
  5. connection.BulkInsert(suppliers).ThenForEach(x => x.Product.SupplierID = x.SupplierID).ThenBulkInsert(x => x.Product);
  6. }

Try it: .NET Core | .NET Framework

Example - Insert with relation (One to Many)

INSERT entities with a one to many relations with Bulk Operation.

  1. DapperPlusManager.Entity<Supplier>().Table("Suppliers").Identity(x => x.SupplierID);
  2. DapperPlusManager.Entity<Product>().Table("Products").Identity(x => x.ProductID);
  3. using (var connection = new SqlConnection(FiddleHelper.GetConnectionStringSqlServerW3Schools()))
  4. {
  5. connection.BulkInsert(suppliers).ThenForEach(x => x.Products.ForEach(y => y.SupplierID = x.SupplierID)).ThenBulkInsert(x => x.Products);
  6. }

Try it: .NET Core | .NET Framework

Real-Life Scenarios

Insert and keep identity value

Your entity has an identity property, but you want to force it to insert a specific value instead. The InsertKeepIdentity option allows you to keep the identity value of your entity.

  1. DapperPlusManager.Entity<Customer>().Table("Customers");
  2. using (var connection = new SqlConnection(FiddleHelper.GetConnectionStringSqlServerW3Schools()))
  3. {
  4. connection.UseBulkOptions(options => options.InsertKeepIdentity = true).BulkInsert(customers);
  5. }

Try it: .NET Core | .NET Framework

Insert without returning the identity value

By default, the BulkInsert method already returns the identity when inserting.

However, such behavior impacts performance. For example, when the identity must be returned, a temporary table is created in SQL Server instead of directly using SqlBulkCopy into the destination table.

You can improve your performance by turning off the AutoMapOutput option.

  1. DapperPlusManager.Entity<Customer>().Table("Customers");
  2. using (var connection = new SqlConnection(FiddleHelper.GetConnectionStringSqlServerW3Schools()))
  3. {
  4. connection.UseBulkOptions(options => options.AutoMapOutputDirection = false).BulkInsert(customers);
  5. FiddleHelper.WriteTable("1 - Customers (from list)", customers);
  6. }

Try it: .NET Core | .NET Framework