using Microsoft.EntityFrameworkCore;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using Microsoft.Data.SqlClient;
public static void Main()
using (var context = new EntityContext())
context.Database.EnsureCreated();
using (var context = new EntityContext())
context.Customers.Add(new Customer() { Code = $"Info", Name = "Support Team", Email = "info@zzzprojects.com", IsActive = true, LastLogin = new DateTime(1981, 04, 13) });
context.Customers.Add(new Customer() { Code = $"Sales", Name = "Sales Team", Email = "sales@zzzprojects.com", 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);
.Where(x => x.IsActive && x.LastLogin < dateToDeactivate)
.UpdateFromQuery(x => new Customer { IsActive = false });
.InsertFromQuery("bck_Customers", x => new { x.CustomerID, x.Code, x.Name, x.Email, x.LastLogin });
FiddleHelper.WriteTable("2 - Customers (After)", context.Customers.ToList());
FiddleHelper.WriteTable("3 - Backup Customers", context.BackupCustomers.ToList());
public class EntityContext : DbContext
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 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; }
public class BackupCustomer
[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; }