Bulk SynchronizeDirectly execute add, update, and delete operations to mirror data in EF Core
The BulkSynchronize
method allows you to mirror data from your source to the database in EF Core. What exactly is a mirror operation?
- Rows that match the entity key are UPDATED.
- Rows that exist in the source but not in the database are INSERTED.
- Rows that exist in the database but not in the source are DELETED.
In other words, your destination table data becomes exactly like the list of entities you provide. The method is similar to BulkMerge, but it also includes the delete step, making it a complete mirror.
It’s also possible to synchronize only a subset of your table with the ColumnSynchronizeDeleteKeySubsetExpression
option, or to soft-delete rows with the SynchronizeSoftDeleteFormula
option (which we will cover later).
// Easy to use context.BulkSynchronize(customers); // Easy to customize context.BulkSynchronize(customers, options => { options.ColumnPrimaryKeyExpression = customer => customer.Code; });
Online Example (EF Core) | Online Example (EF6)
The BulkSynchronize
method is fast but also flexible to let you handle various scenarios in Entity Framework such as:
- Synchronize and keep identity value
- Synchronize and include/exclude properties
- Synchronize a subset of the table
- Synchronize with custom key
- Synchronize with future action
- More scenarios
- All Entity Framework Core Version: EF Core 9, EF Core 8, EF Core 7, EF Core 6, EF Core 5, EF Core 3
- All Entity Framework Version: EF6, EF5, EF4
- All Inheritances (TPC, TPH, TPT)
- Complex Type/Owned Entity Type
- Enum
- Value Converter (EF Core)
- And more!
- Easy to use
- Flexible
- Increase performance
- Increase application responsiveness
- Reduce database load
- Reduce database round-trips
The BulkSynchronize
and BulkSynchronizeAsync
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 EF Core | Try it in EF6
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 EF Core | Try it in EF6
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 EF Core | Try it in EF6
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 EF Core | Try it in EF6
You want to synchronize your table, but only a subset of the table and not the whole table, such as only a specific user, type, or category that exists in the entities list provided.
context.BulkSynchronize(customers, options => { options.ColumnPrimaryKeyExpression = customer => customer.Name; options.ColumnSynchronizeDeleteKeySubsetExpression = customer => customer.Type; });
Try it in EF Core | Try it in EF6
Learn more here: https://entityframework-extensions.net/column-synchronize-delete-key-subset-expression
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 EF Core | Try it in EF6
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 EF Core | Try it in EF6
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
Methods
Name | Description | Example |
---|---|---|
BulkSynchronize<T>(items) |
Bulk synchronize entities in your database. | EFCore / EF6 |
BulkSynchronize<T>(items, options) |
Bulk synchronize entities in your database. | EFCore / EF6 |
BulkSynchronizeAsync<T>(items) |
Bulk synchronize entities asynchronously in your database. | EFCore / EF6 |
BulkSynchronizeAsync<T>(items, cancellationToken) |
Bulk synchronize entities asynchronously in your database. | EFCore / EF6 |
BulkSynchronizeAsync<T>(items, options, cancellationToken) |
Bulk synchronize entities asynchronously in your database. | EFCore / EF6 |
Options
More options can be found here:
- Audit
- Batch
- Column
- Context Factory
- Execute Event
- Identity
- Include Graph
- Key
- Logging
- Temporary Table
- Transaction
- Transient Error
- SQL Server
ZZZ Projects