Skip to content

Instantly share code, notes, and snippets.

View JonathanMagnan's full-sized avatar

Jonathan Magnan JonathanMagnan

View GitHub Profile
using (var ctx = new CurrentContext())
{
var lastLogin = DateTime.Now.AddYears(-2);
var list = ctx.Customers.Where(x => x.LastLogin < lastLogin).ToList();
ctx.Customers.RemoveRange(list);
// HOW to automatically handle soft delete?
ctx.BulkSaveChanges();
}
// 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 */ };
});
}