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", IsLocked = true });
customers.Add(new Customer() { CustomerID = 2, Name = "Mikael", Email = "Email_Mikael", Note = "Note_Mikael", IsLocked = false });
customers.Add(new Customer() { CustomerID = 3, Name = "Sara", Email = "Email_Sara", Note = "Note_Sara", IsLocked = false });
context.BulkInsert(customers, options => { options.InsertKeepIdentity = true; });
FiddleHelper.WriteTable("1 - Before BulkMerge", context.Customers.ToList());
using (var context = new EntityContext())
var customers = new List<Customer>();
customers.Add(new Customer() { CustomerID = 1, Name = "Jonathan (Updated)", Email = "Email_Jonathan (Updated)", Note = "Note_Jonathan (Updated)" });
customers.Add(new Customer() { CustomerID = 2, Name = "Mikael (Updated)", Email = "Email_Mikael (Updated)", Note = "Note_Mikael (Updated)" });
customers.Add(new Customer() { CustomerID = 3, Name = "Sara (Updated)", Email = "Email_Sara (Updated)", Note = "Note_Sara (Updated)"});
customers.ForEach(x => x.IsLocked = false);
context.BulkMerge(customers, options =>
options.IgnoreOnMergeMatchedAndConditionExpression = x => new { x.CustomerID, x.Name, x.Email, x.Note };
FiddleHelper.WriteTable("2 - After BulkMerge", 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 bool IsLocked { get; set; }