Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
using (var ctx = new CurrentContext())
{
var lastLogin = DateTime.Now.AddYears(-2);
var list = ctx.Customers.Where(x => x.LastLogin < lastLogin).ToList();
ctx.Customers.RemoveRange(list);
// HOW to automatically handle soft delete?
ctx.BulkSaveChanges();
}
EntityFrameworkManager.PreBulkSaveChanges = context =>
{
foreach (var entry in context.ChangeTracker.Entries())
{
if (entry.State == EntityState.Deleted && entry.Entity is Customer)
{
entry.State = EntityState.Modified;
((Customer) entry.Entity).IsDeleted = true;
}
}
};
using (var ctx = new CurrentContext())
{
var lastLogin = DateTime.Now.AddYears(-2);
var list = ctx.Customers.Where(x => x.LastLogin < lastLogin).ToList();
ctx.Customers.RemoveRange(list);
ctx.BulkSaveChanges();
}
using (var ctx = new CurrentContext())
{
var lastLogin = DateTime.Now.AddYears(-2);
var list = ctx.Customers.Where(x => x.LastLogin < lastLogin).ToList();
// HOW to automatically handle soft delete?
ctx.BulkDelete(list);
}
public static class Extensions
{
public static void BulkHardOrSoftDelete<T>(this DbContext ctx, IEnumerable<T> items) where T : class
{
var hardDelete = new List<T>();
var softDelete = new List<T>();
foreach (var item in items)
{
var customer = item as Customer;
if (customer != null)
{
customer.IsDeleted = true;
softDelete.Add(item);
}
else
{
hardDelete.Add(item);
}
}
if (hardDelete.Count > 0)
{
ctx.BulkDelete(hardDelete);
}
if (softDelete.Count > 0)
{
ctx.BulkUpdate(softDelete);
}
}
}
using (var ctx = new CurrentContext())
{
var lastLogin = DateTime.Now.AddYears(-2);
var list = ctx.Customers.Where(x => x.LastLogin < lastLogin).ToList();
ctx.BulkHardOrSoftDelete(list);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment