EF Extensions - Home - Main Features - Batch Operations

CSharp · Console

/Program.cs

// Entity Framework Extensions - Main Features - Batch Operations
// Doc: https://entityframework-extensions.net/

// @nuget: Microsoft.EntityFrameworkCore.SqlServer
// @nuget: Z.EntityFramework.Extensions.EFCore

using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using Microsoft.Data.SqlClient;
					
public class Program
{
	public static void Main()
	{
		using (var context = new EntityContext())
		{
			context.Database.EnsureCreated();
		}
		
		// SEED database
		using (var context = new EntityContext())
		{
			context.Customers.Add(new Customer() { Code = $"Info", Name = "Support Team", Email = "[email protected]", IsActive = true, LastLogin = new DateTime(1981, 04, 13) });
			context.Customers.Add(new Customer() { Code = $"Sales", Name = "Sales Team", Email = "[email protected]", IsActive = true, LastLogin = DateTime.Now });
			context.BulkSaveChanges();
			
			FiddleHelper.WriteTable("1 - Customers (Before)", context.Customers.ToList());			
		}
		
		using (var context = new EntityContext())
		{
			var dateToDeactivate = DateTime.Now.AddYears(-2);

			// UPDATE active customers who haven’t logged in recently
			context.Customers
				.Where(x => x.IsActive && x.LastLogin < dateToDeactivate)
				.UpdateFromQuery(x => new Customer { IsActive = false });

			// INSERT inactive customers without invoices into a backup table
			context.Customers
				.Where(x => !x.IsActive)
				.InsertFromQuery("bck_Customers", x => new { x.CustomerID, x.Code, x.Name, x.Email, x.LastLogin });

			// DELETE inactive customers without invoices
			context.Customers
				.Where(x => !x.IsActive)
				.DeleteFromQuery();
			
			FiddleHelper.WriteTable("2 - Customers (After)", context.Customers.ToList());		
			FiddleHelper.WriteTable("3 - Backup Customers", context.BackupCustomers.ToList());	
		}
	}

	public class EntityContext : DbContext
	{
		public EntityContext() 
		{
		}
		
		public DbSet<Customer> Customers { get; set; }
		public DbSet<BackupCustomer> BackupCustomers { get; set; }
		
		protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
		{
			optionsBuilder.UseSqlServer(new SqlConnection(FiddleHelper.GetConnectionStringSqlServer()));

			base.OnConfiguring(optionsBuilder);
		}
	}

	public class Customer
	{
		public int CustomerID { get; set; }
		public string Code { get; set; }
		public string Name { get; set; }
		public string Email { get; set; }
		public bool IsActive { get; set; }
		public DateTime LastLogin { get; set; }
	}
	
	[Table("bck_Customers")]
	public class BackupCustomer
	{
		[Key]
		[DatabaseGenerated(DatabaseGeneratedOption.None)]
		public int CustomerID { get; set; }
		public string Code { get; set; }
		public string Name { get; set; }
		public string Email { get; set; }
		public DateTime LastLogin { get; set; }
	}
}
;