using Microsoft.EntityFrameworkCore;
using System.Collections.Generic;
using Microsoft.Data.SqlClient;
public static void Main()
using (var context = new EntityContext())
context.Database.EnsureCreated();
var customers = new List<Customer>();
customers.Add(new Customer() { CustomerID = 1, Name = "Jonathan", Email = "Email_Jonathan", Note = "Note_Jonathan", Version = 4 });
customers.Add(new Customer() { CustomerID = 2, Name = "Mikael", Email = "Email_Mikael", Note = "Note_Mikael", Version = 2 });
customers.Add(new Customer() { CustomerID = 3, Name = "Sara", Email = "Email_Sara", Note = "Note_Sara", Version = 2 });
context.BulkInsert(customers, options => { options.InsertKeepIdentity = true; });
FiddleHelper.WriteTable("1 - Before BulkDelete", context.Customers.ToList());
using (var context = new EntityContext())
var customers = new List<Customer>();
customers.Add(new Customer() { CustomerID = 1, Version = 2 });
customers.Add(new Customer() { CustomerID = 2, Version = 2 });
customers.Add(new Customer() { CustomerID = 3, Version = 2 });
context.BulkDelete(customers, options =>
options.DeleteMatchedAndConditionExpression = x => new { x.Version };
FiddleHelper.WriteTable("2 - After BulkDelete", context.Customers.AsNoTracking().ToList());
public class EntityContext : DbContext
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
optionsBuilder.UseSqlServer(new SqlConnection(FiddleHelper.GetConnectionStringSqlServer()));
base.OnConfiguring(optionsBuilder);
public DbSet<Customer> Customers { get; set; }
public int CustomerID { get; set; }
public string Name { get; set; }
public string Email { get; set; }
public string Note { get; set; }
public int Version { get; set; }