LinqToSql Plus Bulk Synchronize
Description
SYNCHRONIZE
all entities from the 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
.
The database table becomes a mirror of the entity list provided.
// Easy to use ctx.BulkSynchronize(list); // Easy to customize context.BulkSynchronize(customers, options => options.ColumnPrimaryKeyExpression = customer => customer.Code);
Purpose
Synchronizing
entities with the database is a very rare scenario, but it may happen when two databases need to be synchronized.
BulkSynchronize
gives you the scalability and flexibility required if you encounter this situation.
Performance Comparisons
Operations | 1,000 Entities | 2,000 Entities | 5,000 Entities |
---|---|---|---|
SubmitChanges | 1,000 ms | 2,000 ms | 5,000 ms |
BulkSynchronize | 55 ms | 65 ms | 85 ms |
How can I specify more than one option?
You can specify more than one option using an anonymous block.
context.BulkSynchronize(list, options => { options.BatchSize = 100; options.ColumnInputExpression = c => new {c.ID, c.Name, c.Description}; });
How can I specify the Batch Size?
You can specify a custom batch size using the BatchSize
option.
Read more: BatchSize
context.BulkSynchronize(list, options => options.BatchSize = 100);
How can I specify custom columns to Synchronize?
You can specify custom columns using the ColumnInputExpression
option.
Read more: ColumnInputExpression
context.BulkSynchronize(list, options => options.ColumnInputExpression = c => new {c.Name, c.Description});
How can I specify custom keys to use?
You can specify custom keys using the ColumnPrimaryKeyExpression
option.
Read more: ColumnPrimaryKeyExpression
// Single Key context.BulkSynchronize(customers, options => options.ColumnPrimaryKeyExpression = customer => customer.Code); // Surrogate Key context.BulkSynchronize(customers, options => options.ColumnPrimaryKeyExpression = customer => new { customer.Code1, customer.Code2 });