Entity Framework Extensions Bulk Synchronize

Description

The EF BulkSynchronize extension method let 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
context.BulkSynchronize(customers);

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

Try it in EF6 | Try it in EF Core

Scenarios

The BulkSynchronize method is fast but also flexible to let you handle various scenarios in Entity Framework such as:

What is supported?

  • All Entity Framework versions (EF4, EF5, EF6, EF Core, EF Classic)
  • All Inheritances (TPC, TPH, TPT)
  • Complex Type/Owned Entity Type
  • Enum
  • Value Converter (EF Core)
  • And more!

Advantages

  • Easy to use
  • Flexible
  • Increase performance
  • Increase application responsiveness
  • Reduce database load
  • Reduce database round-trips

Getting Started

Bulk Synchronize

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

context.BulkSynchronize(customers);

context.BulkSynchronizeAsync(customers, cancellationToken);

Try it in EF6 | Try it in EF Core

Bulk Synchronize with options

The options parameter let 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 in EF6 | Try it in EF Core

Real Life Scenarios

Synchronize and keep identity value

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

context.BulkSynchronize(customers, options => options.SynchronizeKeepIdentity = true);

Try it in EF6 | Try it in EF Core

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.
context.BulkSynchronize(customizeToSynchronize, options => {
    options.IgnoreOnSynchronizeInsertExpression = c => c.UpdatedDate;
    options.IgnoreOnSynchronizeUpdateExpression = c => c.CreatedDate;
});

Try it in EF6 | Try it in EF Core

Synchronize with custom key

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

context.BulkSynchronize(customers, options => options.ColumnPrimaryKeyExpression = c => c.Code);

Try it in EF6 | Try it in EF Core

Synchronize with future action

You want to synchronize entities, but you want to defer the execution.

By default, BulkSynchronize is an immediate operation. That means, it's executed as soon as you call the method.

FutureAction: This option lets you defer the execution of a Bulk Synchronize. ExecuteFutureAction: This option trigger and execute all pending FutureAction.

context.FutureAction(x => x.BulkSynchronize(customers));
context.FutureAction(x => x.BulkSynchronize(invoices));

// ...code...

context.ExecuteFutureAction();

Try it in EF6 | Try it in EF Core

More scenarios

Hundreds of scenarios have 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
BulkSynchronize<T>(items) Bulk synchronize entities in your database. EF6 / EFCore
BulkSynchronize<T>(items, options) Bulk synchronize entities in your database. EF6 / EFCore
BulkSynchronizeAsync<T>(items) Bulk synchronize entities asynchronously in your database. EF6 / EFCore
BulkSynchronizeAsync<T>(items, cancellationToken) Bulk synchronize entities asynchronously in your database. EF6 / EFCore
BulkSynchronizeAsync<T>(items, options, cancellationToken) Bulk synchronize entities asynchronously in your database. EF6 / EFCore
Options

More options can be found here: