|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Linq.Expressions; |
| 4 | +using System.Linq; |
| 5 | +using System.Text; |
| 6 | + |
| 7 | +using Microsoft.EntityFrameworkCore.Infrastructure; |
| 8 | + |
| 9 | +using Microsoft.EntityFrameworkCore; |
| 10 | + |
| 11 | +namespace Flithor_ReusableCodes |
| 12 | +{ |
| 13 | + /// <summary> |
| 14 | + /// Make you remove data in ef more easily |
| 15 | + /// </summary> |
| 16 | + public static class EFRemoveExtension |
| 17 | + { |
| 18 | + /// <summary>Query primary key only by condition</summary> |
| 19 | + /// <param name="predicate">Query condition</param> |
| 20 | + public static IQueryable<TEntity> SelectKeyOnly<TEntity>(this DbContext db, Expression<Func<TEntity, bool>> predicate) where TEntity : class |
| 21 | + { |
| 22 | + var entityModel = db.Model.FindEntityType(typeof(TEntity)); |
| 23 | + var keys = entityModel.FindPrimaryKey(); |
| 24 | + var paramE = predicate.Parameters[0]; |
| 25 | + var init = Expression.MemberInit(Expression.New(typeof(TEntity)), keys.Properties.Select(p => Expression.Bind(p.PropertyInfo, Expression.MakeMemberAccess(paramE, p.PropertyInfo)))); |
| 26 | + var lambda = Expression.Lambda<Func<TEntity, TEntity>>(init, paramE); |
| 27 | + return db.Set<TEntity>().Where(predicate).Select(lambda); |
| 28 | + } |
| 29 | + /// <summary>Query primary key only by condition</summary> |
| 30 | + /// <param name="predicate">Query condition</param> |
| 31 | + public static IQueryable<TEntity> SelectKeyOnly<TEntity>(this DbSet<TEntity> set, Expression<Func<TEntity, bool>> predicate) where TEntity : class => |
| 32 | + SelectKeyOnly(set.GetService<ICurrentDbContext>().Context, predicate); |
| 33 | + |
| 34 | + /// <summary>Delete data by condition</summary> |
| 35 | + /// <param name="predicate">Query condition</param> |
| 36 | + public static void RemoveWhere<TEntity>(this DbContext db, Expression<Func<TEntity, bool>> predicate) where TEntity : class |
| 37 | + { |
| 38 | + db.Set<TEntity>().RemoveRange(SelectKeyOnly(db, predicate)); |
| 39 | + } |
| 40 | + /// <summary>Delete data by condition</summary> |
| 41 | + /// <param name="predicate">Query condition</param> |
| 42 | + public static void RemoveWhere<TEntity>(this DbSet<TEntity> set, Expression<Func<TEntity, bool>> predicate) where TEntity : class => |
| 43 | + RemoveWhere(set.GetService<ICurrentDbContext>().Context, predicate); |
| 44 | + |
| 45 | + /// <summary>Delete data by condition and return deleted entities</summary> |
| 46 | + /// <param name="predicate">Query condition</param> |
| 47 | + public static List<TEntity> RemoveWhereAndTake<TEntity>(this DbContext db, Expression<Func<TEntity, bool>> predicate) where TEntity : class |
| 48 | + { |
| 49 | + var entities = db.Set<TEntity>().Where(predicate).ToList(); |
| 50 | + db.RemoveRange(entities); |
| 51 | + return entities; |
| 52 | + } |
| 53 | + /// <summary>Delete data by condition and return deleted entities</summary> |
| 54 | + /// <param name="predicate">Query condition</param> |
| 55 | + public static List<TEntity> RemoveWhereAndTake<TEntity>(this DbSet<TEntity> set, Expression<Func<TEntity, bool>> predicate) where TEntity : class => |
| 56 | + RemoveWhereAndTake(set.GetService<ICurrentDbContext>().Context, predicate); |
| 57 | + |
| 58 | + /// <summary>Delete data by condition and return primary keys of deleted entities</summary> |
| 59 | + /// <param name="predicate">Query condition</param> |
| 60 | + public static List<TEntity> RemoveWhereAndTakeKeys<TEntity>(this DbContext db, Expression<Func<TEntity, bool>> predicate) where TEntity : class |
| 61 | + { |
| 62 | + var keys = SelectKeyOnly(db, predicate).ToList(); |
| 63 | + db.RemoveRange(keys); |
| 64 | + return keys; |
| 65 | + } |
| 66 | + /// <summary>Delete data by condition and return primary keys of deleted entities</summary> |
| 67 | + /// <param name="predicate">Query condition</param> |
| 68 | + public static List<TEntity> RemoveWhereAndTakeKeys<TEntity>(this DbSet<TEntity> set, Expression<Func<TEntity, bool>> predicate) where TEntity : class => |
| 69 | + RemoveWhereAndTakeKeys(set.GetService<ICurrentDbContext>().Context, predicate); |
| 70 | + } |
| 71 | +} |
0 commit comments