Last active
January 12, 2017 03:02
-
-
Save JonathanMagnan/405cb1c24b4990a6b5cbbd9ee99d7a4b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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