File tree Expand file tree Collapse file tree 3 files changed +42
-6
lines changed
samples/WeihanLi.EntityFramework.Sample
src/WeihanLi.EntityFramework Expand file tree Collapse file tree 3 files changed +42
-6
lines changed Original file line number Diff line number Diff line change @@ -21,10 +21,10 @@ public static class Program
2121 public static async Task Main ( string [ ] args )
2222 {
2323 // SoftDeleteTest();
24- // RepositoryTest();
24+ RepositoryTest ( ) ;
2525 // AutoAuditTest();
2626
27- await DbContextInterceptorSamples . RunAsync ( ) ;
27+ // await DbContextInterceptorSamples.RunAsync();
2828
2929 Console . WriteLine ( "completed" ) ;
3030 Console . ReadLine ( ) ;
@@ -212,7 +212,7 @@ private static void RepositoryTest()
212212 var conn = db . Database . GetDbConnection ( ) ;
213213 try
214214 {
215- conn . Execute ( $@ "TRUNCATE TABLE { tableName } ") ;
215+ conn . Execute ( $ "TRUNCATE TABLE { tableName } ") ;
216216 }
217217 catch
218218 {
@@ -236,6 +236,10 @@ private static void RepositoryTest()
236236 { "Extra" , "12345" }
237237 } ) ;
238238
239+ var list = repo . Query ( q => q . IgnoreQueryFilters ( [ "not-null" ] ) )
240+ . ToArray ( ) ;
241+ Console . WriteLine ( list . Length ) ;
242+
239243 repo . Update ( x => x . SetProperty ( _ => _ . Extra , _ => "{}" ) , q => q . IgnoreQueryFilters ( ) ) ;
240244
241245 var abc = db . TestEntities . AsNoTracking ( ) . ToArray ( ) ;
Original file line number Diff line number Diff line change @@ -11,6 +11,15 @@ public TestDbContext(DbContextOptions<TestDbContext> options) : base(options)
1111 {
1212 }
1313
14+ protected override void OnModelCreating ( ModelBuilder modelBuilder )
15+ {
16+ base . OnModelCreating ( modelBuilder ) ;
17+ modelBuilder . Entity < TestEntity > ( )
18+ // .HasQueryFilter("one-month-ago", t => t.CreatedAt > DateTime.Now.AddMonths(-1))
19+ . HasQueryFilter ( "not-null" , t => t . Extra != null )
20+ ;
21+ }
22+
1423 public DbSet < TestEntity > TestEntities { get ; set ; } = null ! ;
1524}
1625
Original file line number Diff line number Diff line change 11using Microsoft . EntityFrameworkCore ;
22using Microsoft . EntityFrameworkCore . Query ;
3- using System ;
4- using System . Collections . Generic ;
5- using System . Linq ;
63using System . Linq . Expressions ;
74using WeihanLi . Common ;
85
@@ -55,6 +52,28 @@ public EFRepositoryQueryBuilder<TEntity> IgnoreQueryFilters(bool ignoreQueryFilt
5552 _ignoreQueryFilters = ignoreQueryFilters ;
5653 return this ;
5754 }
55+
56+ private readonly HashSet < string > _queryFiltersToIgnore = new ( ) ;
57+
58+ public EFRepositoryQueryBuilder < TEntity > IgnoreQueryFilters ( IReadOnlyCollection < string > queryFilters , bool ignoreQueryFilters = true )
59+ {
60+ ArgumentNullException . ThrowIfNull ( queryFilters ) ;
61+ if ( ignoreQueryFilters )
62+ {
63+ foreach ( var queryFilter in queryFilters )
64+ {
65+ _queryFiltersToIgnore . Add ( queryFilter ) ;
66+ }
67+ }
68+ else
69+ {
70+ foreach ( var queryFilter in queryFilters )
71+ {
72+ _queryFiltersToIgnore . Remove ( queryFilter ) ;
73+ }
74+ }
75+ return this ;
76+ }
5877
5978 private int _count ;
6079
@@ -83,6 +102,10 @@ public IQueryable<TEntity> Build()
83102 {
84103 query = query . IgnoreQueryFilters ( ) ;
85104 }
105+ else if ( _queryFiltersToIgnore . Count > 0 )
106+ {
107+ query = query . IgnoreQueryFilters ( _queryFiltersToIgnore ) ;
108+ }
86109 if ( _whereExpression . Count > 0 )
87110 {
88111 foreach ( var expression in _whereExpression )
You can’t perform that action at this time.
0 commit comments