.NET Bulk Operations Bulk Synchronize

Description

The BulkSynchronize extension method lets you synchronize a large number of entities in your database.

A synchronize is a mirror operation from the data source to the database. All rows that match the entity key are UPDATED, non-matching rows that exist from the source are INSERTED, non-matching rows that exist in the database are DELETED.

// Easy to use
bulk.BulkSynchronize(customers);

// Easy to customize
bulk.DestinationTableName = "Customers";

bulk.AutoMapKeyExpression = customer => customer.Code;
bulk.BulkSynchronize(customers);

Try it (Entity): .NET Core | .NET Framework

// Easy to use
bulk.BulkSynchronize(dtCustomers);

// Easy to customize
bulk.DestinationTableName = "Customers";

bulk.AutoMapKeyName = "Code";
bulk.BulkSynchronize(dtCustomers);
});

Try it (DataTable): .NET Core | .NET Framework

Scenarios

The BulkSynchronize 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 Synchronize

The BulkSynchronize and BulkSynchronizeAync methods let you synchronize a large number of entities in your database.

bulk.AutoMapKeyExpression = customer => customer.Code;
bulk.BulkSynchronize(customers);

Try it (Entity): .NET Core | .NET Framework

bulk.AutoMapKeyName = "Code";
bulk.BulkSynchronize(dtCustomers);

Try it (DataTable): .NET Core | .NET Framework

Bulk Synchronize with options

The options parameter lets you use a lambda expression to customize the way entities are synchronized.

context.BulkSynchronize(customers, options => options.BatchSize = 100);

context.BulkSynchronize(customers, options => {
    options.ColumnPrimaryKeyExpression = customer => customer.Code;
});

Try it (Entity): .NET Core | .NET Framework

Try it (DataTable): .NET Core | .NET Framework

Real-Life Scenarios

Synchronize and keep identity value

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

bulk.SynchronizeKeepidentity = true;
bulk.BulkSynchronize(customers);

Try it (Entity): .NET Core | .NET Framework

Try it (DataTable): .NET Core | .NET Framework

Synchronize and include/exclude properties

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

  • ColumnInputExpression: This option lets you choose which properties to map.
  • IgnoreOnSynchronizeInsertExpression: This option lets you ignore when inserting properties that are auto-mapped.
  • IgnoreOnSynchronizeUpdateExpression: This option lets you ignore when updating properties that are auto-mapped.
bulk.IgnoreOnSynchronizeInsertExpression = c => c.UpdatedDate;
bulk.IgnoreOnSynchronizeUpdateExpression = c => c.CreatedDate;
bulk.AutoMapKeyExpression = customer => customer.Code;
bulk.BulkSynchronize(customizeToSynchronize);

Try it (Entity): .NET Core | .NET Framework

var columnMapping1 = new ColumnMapping("UpdatedDate");
var columnMapping2 = new ColumnMapping("CreatedDate");

columnMapping1.IgnoreOnSynchronizeInsert = true;
columnMapping2.IgnoreOnSynchronizeUpdate = true;
bulk.ColumnMappings.Add(columnMapping1);
bulk.ColumnMappings.Add(columnMapping2);

bulk.ColumnMappings.Add("Code", true);
bulk.ColumnMappings.Add("CustomerID");
bulk.ColumnMappings.Add("FirstName");
bulk.ColumnMappings.Add("LastName");

bulk.BulkSynchronize(dtCustomers);

Try it (DataTable): .NET Core | .NET Framework

Synchronize with custom key

You want to synchronize entities, but you don't have the primary key. The ColumnPrimaryKeyExpression lets you use as a key any property or combination of properties.

bulk.AutoMapKeyExpression = customer => customer.Code;
bulk.BulkSynchronize(customers);

Try it (Entity): .NET Core | .NET Framework

bulk.AutoMapKeyName = "Code";
bulk.BulkSynchronize(dtCustomers);

Try it (DataTable): .NET Core | .NET Framework

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

BulkSynchronize

Methods
Name Description Example (DataTable) Example (Entity)
BulkSynchronize<T>(items) Bulk synchronize entities in your database. .NET Core / .NET Framework .NET Core / .NET Framework
BulkSynchronizeAsync<T>(items) Bulk synchronize entities asynchronously in your database.
BulkSynchronizeAsync<T>(items, cancellationToken) Bulk synchronize entities asynchronously in your database.
Options

More options can be found here: