EF.Plus - Batch Delete - Batch Delay Interval

CSharp · Console

/Program.cs

// Entity Framework Plus
// Doc: https://entityframework-plus.net/batch-delete

// @nuget: EntityFramework
// @nuget: Z.EntityFramework.Plus.EF6

using System;
using System.Linq;
using System.Data.Entity;
using Z.EntityFramework.Plus;

public class Program
{
	public static void Main()
	{
		GenerateData();
		
		// Delete: DELETE all rows from the database using a LINQ Query without loading entities in the context
		using (var context = new EntityContext())
		{
			// BatchSize
			context.Customers.Delete(x => x.BatchDelayInterval = 2000);
		}
		
		using (var context = new EntityContext())
		{
			FiddleHelper.WriteTable(context.Customers.ToList());	
		}
	}
		 
	public static void GenerateData()
	{
		
		using (var context = new EntityContext())
		{
			context.Customers.Add(new Customer() { Name ="Customer_A", IsActive = false  });
			context.Customers.Add(new Customer() { Name ="Customer_B", IsActive = true });
			context.Customers.Add(new Customer() { Name ="Customer_C", IsActive = false });

			context.SaveChanges();	
		}
		
		using (var context = new EntityContext())
		{
			FiddleHelper.WriteTable(context.Customers.ToList());	
		}
	}

	public class EntityContext : DbContext
	{
		public EntityContext() : base(FiddleHelper.GetConnectionStringSqlServer())
		{

		}
		
		public DbSet<Customer> Customers { get; set; }
	}
	
	
	public class Customer
	{
		public int CustomerID { get; set; }
		public string Name { get; set; }
		public Boolean IsActive { get; set; }
	}
}
;