using Microsoft.EntityFrameworkCore;
using System.Collections.Generic;
using Microsoft.Data.SqlClient;
using Z.EntityFramework.Extensions;
public static void Main()
using (var context = new EntityContext())
context.Database.EnsureCreated();
EntityFrameworkManager.PreBulkUpdate = (ctx, obj) =>
if (obj is IEnumerable<Customer>)
foreach (var customer in (IEnumerable<Customer>)obj)
customer.ModifiedDate = DateTime.Now;
using (var context = new EntityContext())
FiddleHelper.WriteTable("Before_BulkMerge", context.Customers.ToList());
var list = context.Customers.ToList();
list.ForEach(x => { x.Name += "_Updated"; x.IsActive = false; });
context.BulkUpdate(list);
using (var context = new EntityContext())
FiddleHelper.WriteTable("After_BulkMerge", context.Customers.ToList());
public static void GenerateData()
using (var context = new EntityContext())
context.Customers.Add(new Customer() { Name ="Customer_A", IsActive = true });
context.Customers.Add(new Customer() { Name ="Customer_B", IsActive = true });
context.BulkSaveChanges();
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 Description { get; set; }
public Boolean IsActive { get; set; }
public DateTime? CreatedDate { get; set; }
public DateTime? ModifiedDate { get; set; }