DbSet
IDeletable
bool IsDeleted
OnModelCreating
mb.ConfigureQueryFiltersForDeletables();
internal interface IDeletable { bool IsDeleted { get; set; } } public static void ConfigureQueryFiltersForDeletables( this ModelBuilder mb ) { /* TODO: Replace MyDataModel */ var types = typeof(MyDataModel) .GetProperties() .Where(x => x.PropertyType.IsGenericType && x.PropertyType.GetGenericTypeDefinition() == typeof(DbSet<>) ) .Select(x => x.PropertyType.GenericTypeArguments.First()) .Where(x => x.GetInterfaces().Contains(typeof(IDeletable)) ) .Distinct() .ToList(); foreach (var type in types) { dynamic dummyInstance = Activator.CreateInstance(type); ConfigureQueryFilterForDeletable(mb, dummyInstance); } } private static void ConfigureQueryFilterForDeletable<TEntity>( ModelBuilder mb, TEntity item ) where TEntity : class, IDeletable { mb.Entity<TEntity>().HasQueryFilter(a => !a.IsDeleted); }
ScrewTurn Wiki version 3.0.1.400. Some of the icons created by FamFamFam. Except where noted, all contents Copyright © 1999-2024, Patrick Jasinski.