Skip to content

Instantly share code, notes, and snippets.

// UPDATE all customers that are inactive for more than two years
context.Customers
.Where(x => x.Actif && x.LastLogin < DateTime.Now.AddYears(-2))
.UpdateFromQuery(x => new Customer {Actif = false});
// Single Key
ctx.BulkUpdate(customers, operation => operation.ColumnPrimaryKeyExpression =
customer => customer.Code);
// Surrogate Key (with anonymous type)
ctx.BulkUpdate(customers, operation => operation.ColumnPrimaryKeyExpression =
customer => new { customer.Code1, customer.Code2, customer.Code3 });
// DON'T add the key if auto-generated
ctx.BulkInsert(customers, operation => operation.ColumnInputExpression =
customer => new {customer.Name, customer.Email});
// ALWAYS add the key
ctx.BulkUpdate(customers, operation => operation.ColumnInputExpression =
customer => new { customer.ID, customer.Name, customer.Email });
// ALWAYS add the key
ctx.BulkMerge(customers, operation => operation.ColumnInputExpression =
public Benchmark()
{
// BENCHMARK using Stopwatch
var clock1 = new Stopwatch();
var clock2 = new Stopwatch();
var nbRecord = 1000;
var nbTry = 5;
var list = GenerateData(nbRecord);
context.BulkSaveChanges(operation =>
{
bulk.SqlBulkCopyOptions = SqlBulkCopyOptions.Default | SqlBulkCopyOptions.TableLock;
});
using (var ctx = new EntitiesContext())
{
ctx.BulkSaveChanges(operation =>
{
operation.RetryCount = 3;
});
}
StringBuilder logger = new StringBuilder();
using (var ctx = new EntitiesContext())
{
ctx.BulkSaveChanges(operation =>
{
operation.Log += s => logger.AppendLine(s);
});
}
using (var ctx = new EntitiesContext())
{
ctx.BulkSaveChanges(operation =>
{
operation.BulkOperationExecuting = bulkOperation => { /* configuration */ };
});
}