using Microsoft.EntityFrameworkCore;
using System.Collections.Generic;
using Microsoft.Data.SqlClient;
public static void Main()
using (var context = new EntityContext())
context.Database.EnsureCreated();
using (var context = new EntityContext())
var customers = context.Customers.ToList();
customers.ForEach(x => { x.Name += "_Updated"; x.Description += "_Updated"; x.CreatedDate = DateTime.Now; x.ModifiedDate = DateTime.Now; x.IsActive = false; });
context.BulkUpdate(customers, options =>
options.IgnoreOnUpdateExpression = customer => new {customer.CustomerID, customer.CreatedDate};
using (var context = new EntityContext())
FiddleHelper.WriteTable("After BulkUpdate",context.Customers.ToList());
public static void GenerateData()
using (var context = new EntityContext())
context.Customers.Add(new Customer() { Name = "Carson", Description = "Description of Carson", CreatedDate = DateTime.Now.AddDays(-3), ModifiedDate = DateTime.Now.AddDays(-3), IsActive = true });
context.Customers.Add(new Customer() { Name = "Meredith", Description = "Description of Meredith", CreatedDate = DateTime.Now.AddDays(-2), ModifiedDate = DateTime.Now.AddDays(-2), IsActive = true });
context.Customers.Add(new Customer() { Name = "Arturo", Description = "Description of Arturo", CreatedDate = DateTime.Now.AddDays(-1), ModifiedDate = DateTime.Now.AddDays(-1), IsActive = true });
context.BulkSaveChanges();
FiddleHelper.WriteTable("Before BulkUpdate",context.Customers.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 Description { get; set; }
public DateTime? ModifiedDate { get; set; }
public DateTime CreatedDate { get; set; }
public Boolean IsActive { get; set; }