using System.Collections.Generic;
using System.Diagnostics;
using System.Data.Entity;
using System.Threading.Tasks;
public static List<BenchmarkResult> BenchmarkResults = new List<BenchmarkResult>();
public static void Main()
public static async Task MainAsync()
var customers = GenerateCustomers(1000);
var clockSaveChanges = new Stopwatch();
var clockBulkSaveChanges = new Stopwatch();
using (var context = new EntityContext())
context.Customers.AddRange(customers);
clockSaveChanges.Start();
BenchmarkResults.Add(new BenchmarkResult() { Action = "SaveChangesAsync (Entity Framework)", Entities = customers.Count, Performance = clockSaveChanges.ElapsedMilliseconds + " ms" });
using (var context = new EntityContext())
context.Customers.AddRange(customers);
clockBulkSaveChanges.Start();
await context.BulkSaveChangesAsync(options => options.BatchSize = 100);
clockBulkSaveChanges.Stop();
BenchmarkResults.Add(new BenchmarkResult() { Action = "BulkSaveChangesAsync", Entities = customers.Count, Performance = clockBulkSaveChanges.ElapsedMilliseconds + " ms" });
FiddleHelper.WriteTable("EFE - High-performance bulk operations", BenchmarkResults);
public static async Task JustInTime_Compile()
var customers = GenerateCustomers(20);
using (var context = new EntityContext())
context.Customers.AddRange(customers);
await context.SaveChangesAsync();
context.Customers.RemoveRange(customers);
await context.SaveChangesAsync();
using (var context = new EntityContext())
context.Customers.AddRange(customers);
await context.BulkSaveChangesAsync(options => options.BatchSize = 100);
context.Customers.RemoveRange(customers);
await context.BulkSaveChangesAsync(options => options.BatchSize = 100);
public static List<Customer> GenerateCustomers(int count)
var list = new List<Customer>();
for(int i = 0; i < count; i++)
list.Add(new Customer() { Name = "Customer_" + i, Description = "Description_" + i, IsActive = i % 2 == 0 });
public class EntityContext : DbContext
public EntityContext() : base(FiddleHelper.GetConnectionStringSqlServer())
public DbSet<Customer> Customers { get; set; }
public int CustomerID { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public Boolean IsActive { get; set; }
public class BenchmarkResult
public string Action { get; set; }
public int Entities { get; set; }
public string Performance { get; set; }