.NET Bulk Operations Bulk Insert

Description

The BulkInsert method let you insert a large number of entities in your database.

// Easy to use
bulk.DestinationTableName = "Customers";
bulk.BulkInsert(customers);

// Easy to customize
bulk.DestinationTableName = "Customers";
bulk.InsertIfNotExists = true;
bulk.AutoMapOutputIdentity = true;
bulk.BulkInsert(customers);

Try it (DataTable)

Try it (Entity)

Performance View

Operations 1,000 Entities 2,000 Entities 5,000 Entities
BulkInsert 50 ms 55 ms 75 ms

Try this benchmark online (DataTable)

Try this benchmark online (Entity)

HINT:

A lot of factors might affect the benchmark time such as index, column type, latency, throttling, etc.

Scenarios

The BulkInsert method is fast but also flexible to let you handle various scenarios such as:

Advantages

  • Easy to use
  • Flexible
  • Increase performance
  • Increase application responsiveness

Getting Started

Bulk Insert

The BulkInsert and BulkInsertAync let you insert a large number of entities in your database.

bulk.BulkInsert(customers);

bulk.BulkInsertAsync(customers, cancellationToken);

Try it (DataTable)

Try it (Entity)

Bulk Insert with options

The options parameter let you customize the way entities are inserted.

bulk.BatchSize = 100;
bulk.BulkInsert(customers);

bulk.PrimaryKeyExpression = customer => customer.Code;
bulk.InsertIfNotExists = true;
bulk.BulkInsert(customers);

Try it (DataTable)

Try it (Entity)

Real Life Scenarios

Insert and keep identity value

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

bulk.DestinationTableName = "Customers";
bulk.InsertKeepIdentity = true;
bulk.BulkInsert(customers);

Try it (DataTable)

Try it (Entity)

Insert and include/exclude properties

You want to insert your entities but only for specific properties.

  • ColumnInputExpression: This option let you choose which properties to map.
  • IgnoreOnInsertExpression: This option let you ignore properties that are auto-mapped.
bulk.ColumnInputExpression = c => new { c.CustomerID, c.Name};
bulk.BulkInsert(customers);
            
bulk.IgnoreOnInsertExpression = c => new { c.ColumnToIgnore };
bulk.BulkInsert(customers);

Try it (DataTable)

Try it (Entity)

Insert only if the entity not already exists

You want to insert entities but only those that don't already exist in the database.

  • InsertIfNotExists: This option let you insert only entity that doesn't already exists.
  • PrimaryKeyExpression: This option let you customize the key to use to check if the entity already exists or not. This option disable the Auto Mapping.
  • AutoMapKeyExpression: This option let you customize the key with an expression and keep the Auto Mapping.
  • AutoMapKeyName: This option let you customize the key by names and keep the Auto Mapping.
bulk.InsertIfNotExists = true;
bulk.AutoMapKeyExpression = c => c.Code;
bulk.BulkInsert(customers);

Try it (Entity)

bulk.InsertIfNotExists = true;
bulk.AutoMapKeyName = "Code";
bulk.BulkInsert(customers);

Try it (DataTable)

You want to insert related child entities.

bulk.AutoMapOutputIdentity = true;
bulk.BulkInsert(invoices);

// SET foreign key value			
invoices.ForEach(x => x.Items.ForEach(y => y.InvoiceID = x.InvoiceID));
bulk.BulkInsert(invoices.SelectMany(x => x.Items).ToList());

Try it (Entity)

Try it (DataTable)

Insert with returning identity value

By default, the BulkInsert method doesn't returns the identity when inserting.

You can return the identity by specifying it should be returned.

bulk.AutoMapOutputIdentity = true;
bulk.BulkInsert(customers);

Try it (DataTable)

Try it (Entity)

More scenarios

Hundred of scenarios has been solved and are now supported.

The best way to ask for a special request or to find out if a solution for your scenario already exists is by contacting us: info@zzzprojects.com

Documentation

BulkInsert

Methods
Name Description Example (DataTable) Example (Entity)
BulkInsert<T>(items) Bulk insert entities in your database. Try it (Entity) Try it (DataTable)
BulkInsertAsync<T>(items) Bulk insert entities asynchronously in your database.
BulkInsertAsync<T>(items, cancellationToken) Bulk insert entities asynchronously in your database.
Options

More options can be found here: