using Microsoft.EntityFrameworkCore;
using System.Collections.Generic;
using Microsoft.Data.SqlClient;
using System.Diagnostics;
using PhenX.EntityFrameworkCore.BulkInsert.Extensions;
using PhenX.EntityFrameworkCore.BulkInsert.SqlServer;
public static List<BenchmarkResult> BenchmarkResults = new List<BenchmarkResult>();
public static void Main()
using (var context = new EntityContext())
context.Database.EnsureCreated();
var customers = GenerateCustomers(10000);
var clockBulkInsertOptimized = new Stopwatch();
var clockBulkInsert = new Stopwatch();
using (var context = new EntityContext())
clockBulkInsertOptimized.Start();
context.BulkInsertOptimized(customers);
clockBulkInsertOptimized.Stop();
BenchmarkResults.Add(new BenchmarkResult() { Action = "BulkInsertOptimized", Entities = customers.Count, Performance = clockBulkInsertOptimized.ElapsedMilliseconds + " ms" });
using (var context = new EntityContext())
context.Customers.ExecuteBulkInsert(customers);
BenchmarkResults.Add(new BenchmarkResult() { Action = "PhenxBulk", Entities = customers.Count, Performance = clockBulkInsert.ElapsedMilliseconds + " ms" });
FiddleHelper.WriteTable("EFE - High-performance bulk operations", BenchmarkResults);
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
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
optionsBuilder.UseSqlServer(new SqlConnection(FiddleHelper.GetConnectionStringSqlServer())).UseBulkInsertSqlServer();
base.OnConfiguring(optionsBuilder);
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 string Column1 { get; set; }
public string Column2 { get; set; }
public string Column3 { get; set; }
public string Column4 { get; set; }
public string Column5 { get; set; }
public string Column6 { get; set; }
public string Column7 { get; set; }
public string Column8 { get; set; }
public string Column9 { get; set; }
public class BenchmarkResult
public string Action { get; set; }
public int Entities { get; set; }
public string Performance { get; set; }