using Microsoft.EntityFrameworkCore;
using System.Collections.Generic;
using Microsoft.Data.SqlClient;
using System.Diagnostics;
public static List<BenchmarkResult> BenchmarkResults = new List<BenchmarkResult>();
public static void Main()
using (var context = new EntityContext())
context.Database.EnsureCreated();
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 = "SaveChanges (Entity Framework)", Entities = customers.Count, Performance = clockSaveChanges.ElapsedMilliseconds + " ms"});
using (var context = new EntityContext())
context.Customers.AddRange(customers);
clockBulkSaveChanges.Start();
context.BulkSaveChanges();
clockBulkSaveChanges.Stop();
BenchmarkResults.Add(new BenchmarkResult()
{Action = "BulkSaveChanges", Entities = customers.Count, Performance = clockBulkSaveChanges.ElapsedMilliseconds + " ms"});
FiddleHelper.WriteTable("EFE - High-performance bulk operations", BenchmarkResults);
public static void JustInTime_Compile()
var customers = GenerateCustomers(20);
using (var context = new EntityContext())
context.Customers.AddRange(customers);
context.Customers.RemoveRange(customers);
using (var context = new EntityContext())
context.Customers.AddRange(customers);
context.BulkSaveChanges();
context.Customers.RemoveRange(customers);
context.BulkSaveChanges();
public static List<Customer> GenerateCustomers(int count)
var list = new List<Customer>();
for (int i = 0; i < count; i++)
{Name = "Customer_" + i, Description = "Description_" + i, IsActive = i % 2 == 0});
public class EntityContext : DbContext
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
optionsBuilder.UseSqlServer(new SqlConnection(FiddleHelper.GetConnectionStringSqlServer()));
base.OnConfiguring(optionsBuilder);
public DbSet<Customer> Customers
public string Description
public class BenchmarkResult
public string Performance