From a7a7ff1c94bf2ecee4fc1c9fc638ff544400608d Mon Sep 17 00:00:00 2001 From: maliming Date: Wed, 14 Oct 2026 14:05:59 +0800 Subject: [PATCH 1/5] Fail closed when data filters cannot be applied to compiled queries --- .../infrastructure/data-filtering.md | 12 +++- .../release-info/migration-guides/abp-10-7.md | 19 ++++++ .../AbpEfCoreModelBuilderExtensions.cs | 13 +++- .../AbpDbContextOptionsExtension.cs | 4 ++ .../EntityFrameworkCore/AbpQueryCompiler.cs | 61 +++++++++++++++++++ .../CompiledQueryCacheMultiTenantRow.cs | 11 ++++ .../CompiledQueryCacheSoftDeleteRow.cs | 11 ++++ .../CompiledQueryCacheTestBase.cs | 25 ++++++++ .../CompiledQueryCacheTestDbContext.cs | 4 ++ .../CompiledQueryCache_Tests.cs | 52 ++++++++++++++++ ...piledQueryCache_WithoutDbFunction_Tests.cs | 25 ++++++++ 11 files changed, 234 insertions(+), 3 deletions(-) create mode 100644 framework/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/EntityFrameworkCore/AbpQueryCompiler.cs create mode 100644 framework/test/Volo.Abp.EntityFrameworkCore.Tests/Volo/Abp/EntityFrameworkCore/DataFiltering/CompiledQueryCache/CompiledQueryCacheMultiTenantRow.cs create mode 100644 framework/test/Volo.Abp.EntityFrameworkCore.Tests/Volo/Abp/EntityFrameworkCore/DataFiltering/CompiledQueryCache/CompiledQueryCacheSoftDeleteRow.cs diff --git a/docs/en/framework/infrastructure/data-filtering.md b/docs/en/framework/infrastructure/data-filtering.md index a2b63f7079..e0b23bcb70 100644 --- a/docs/en/framework/infrastructure/data-filtering.md +++ b/docs/en/framework/infrastructure/data-filtering.md @@ -294,7 +294,11 @@ protected override Expression> CreateFilterExpression() == true) + // Never treat a missing DbContext as "filter disabled", otherwise the query runs without the filter. + var dbContext = abpEfCoreCurrentDbContext.Context + ?? throw new AbpException("The data filter cannot be applied to a query that is compiled outside of AbpEntityQueryProvider."); + + if (dbContext.DataFilter.IsEnabled()) { // isActive == true return new SqlBinaryExpression( @@ -342,6 +346,12 @@ public override string GetCompiledQueryCacheKey() Include any other values read at compilation time in the cache key as well. Keep the call to `base.GetCompiledQueryCacheKey()` to preserve ABP's filter-state keys. +##### Limitations of User-defined function mapping + +The filter state is read while EF Core translates the query. ABP can only provide it for queries that run through `AbpEntityQueryProvider`, such as normal LINQ queries. + +Compiled queries (`EF.CompileQuery` and `EF.CompileAsyncQuery`) are always translated without it, even when they first run inside another query, so ABP throws an `AbpException` for them instead of running them without the data filters. Run such queries as normal LINQ queries, or set `UseDbFunction` of `AbpEfCoreGlobalFilterOptions` to `false`. Your custom `HasTranslation` callbacks should also throw when `AbpEfCoreCurrentDbContext.Context` is `null`, as the example above does. + ##### Make User-defined function mapping compatible with [DevExtreme.AspNet.Data's](https://github.com/DevExpress/DevExtreme.AspNet.Data) `IAsyncAdapter` If you are using [DevExtreme.AspNet.Data](https://github.com/DevExpress/DevExtreme.AspNet.Data) and `User-defined function mapping`, You need to create a custom adapter to compatible with `IAsyncAdapter`. diff --git a/docs/en/release-info/migration-guides/abp-10-7.md b/docs/en/release-info/migration-guides/abp-10-7.md index cf20765a4a..f97c762d74 100644 --- a/docs/en/release-info/migration-guides/abp-10-7.md +++ b/docs/en/release-info/migration-guides/abp-10-7.md @@ -255,6 +255,25 @@ Add any other value the callback reads at compilation time to the key the same w > See the [Data Filtering](../../framework/infrastructure/data-filtering.md) document and [#26282](https://github.com/abpframework/abp/pull/26282) for details. +### Compiled Queries with User-defined Function Mapping + +**Who is affected** + +- Applications that use `EF.CompileQuery` or `EF.CompileAsyncQuery` for entities that implement `ISoftDelete` or `IMultiTenant`, while `AbpEfCoreGlobalFilterOptions.UseDbFunction` is enabled. It is enabled by default for the SQL Server, MySQL, PostgreSQL, Oracle and SQLite providers. +- Applications with custom `HasTranslation` filters that read `AbpEfCoreCurrentDbContext.Context`. + +**What changed** + +- The built-in filters read the filter state from `AbpEfCoreCurrentDbContext` while EF Core translates the query, and only `AbpEntityQueryProvider` sets it. A compiled query is translated without it, and the filters were treated as disabled. Such a query returned soft deleted rows and the rows of other tenants. +- The soft delete and multi-tenant filters now throw an `AbpException` for such a query instead of running it without the filters, even when the compiled query first runs inside another query. + +**What to do** + +- Run the compiled queries as normal LINQ queries, or set `UseDbFunction` of `AbpEfCoreGlobalFilterOptions` to `false`. +- Make your custom `HasTranslation` callbacks throw when `AbpEfCoreCurrentDbContext.Context` is `null`, instead of treating it as a disabled filter. + +> See the [Data Filtering](../../framework/infrastructure/data-filtering.md) document and [#26282](https://github.com/abpframework/abp/pull/26282) for details. + ### Dependency Updates **Who is affected** diff --git a/framework/src/Volo.Abp.EntityFrameworkCore/Microsoft/Extensions/DependencyInjection/AbpEfCoreModelBuilderExtensions.cs b/framework/src/Volo.Abp.EntityFrameworkCore/Microsoft/Extensions/DependencyInjection/AbpEfCoreModelBuilderExtensions.cs index 4b90d801e6..6fccd33502 100644 --- a/framework/src/Volo.Abp.EntityFrameworkCore/Microsoft/Extensions/DependencyInjection/AbpEfCoreModelBuilderExtensions.cs +++ b/framework/src/Volo.Abp.EntityFrameworkCore/Microsoft/Extensions/DependencyInjection/AbpEfCoreModelBuilderExtensions.cs @@ -19,7 +19,7 @@ public static class AbpEfCoreModelBuilderExtensions var isDeleted = args[0]; var boolParam = args[1]; - if (abpEfCoreCurrentDbContext.Context?.DataFilter.IsEnabled() == true) + if (GetCurrentDbContext(abpEfCoreCurrentDbContext).DataFilter.IsEnabled()) { // IsDeleted == false. Use the column's TypeMapping so any ValueConverter is applied to the literal. return new SqlBinaryExpression( @@ -47,7 +47,7 @@ public static class AbpEfCoreModelBuilderExtensions var currentTenantId = args[1]; var boolParam = args[2]; - if (abpEfCoreCurrentDbContext.Context?.DataFilter.IsEnabled() == true) + if (GetCurrentDbContext(abpEfCoreCurrentDbContext).DataFilter.IsEnabled()) { // TenantId == CurrentTenantId return new SqlBinaryExpression( @@ -64,4 +64,13 @@ public static class AbpEfCoreModelBuilderExtensions return modelBuilder; } + + // The filter state is read while EF Core translates the query, and only AbpEntityQueryProvider sets the current DbContext for that. + // Queries compiled without it, such as EF.CompileQuery, must fail instead of running without the data filters. + private static IAbpEfCoreDbFunctionContext GetCurrentDbContext(AbpEfCoreCurrentDbContext abpEfCoreCurrentDbContext) + { + return abpEfCoreCurrentDbContext.Context ?? throw new AbpException( + "The data filters cannot be applied to a query that is compiled outside of AbpEntityQueryProvider, such as EF.CompileQuery or EF.CompileAsyncQuery. " + + "Run it as a normal LINQ query, or set 'UseDbFunction' of 'AbpEfCoreGlobalFilterOptions' to false."); + } } diff --git a/framework/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/EntityFrameworkCore/AbpDbContextOptionsExtension.cs b/framework/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/EntityFrameworkCore/AbpDbContextOptionsExtension.cs index d9dc4cef9f..5fdb4fe817 100644 --- a/framework/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/EntityFrameworkCore/AbpDbContextOptionsExtension.cs +++ b/framework/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/EntityFrameworkCore/AbpDbContextOptionsExtension.cs @@ -3,6 +3,7 @@ using System.Collections.Generic; using System.Linq; using Microsoft.EntityFrameworkCore.Infrastructure; using Microsoft.EntityFrameworkCore.Query; +using Microsoft.EntityFrameworkCore.Query.Internal; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection.Extensions; using Volo.Abp.EntityFrameworkCore.GlobalFilters; @@ -25,6 +26,9 @@ public class AbpDbContextOptionsExtension : IDbContextOptionsExtension } services.Replace(ServiceDescriptor.Scoped()); +#pragma warning disable EF1001 + services.Replace(ServiceDescriptor.Scoped()); +#pragma warning restore EF1001 services.AddSingleton(typeof(AbpEfCoreCurrentDbContext)); } diff --git a/framework/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/EntityFrameworkCore/AbpQueryCompiler.cs b/framework/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/EntityFrameworkCore/AbpQueryCompiler.cs new file mode 100644 index 0000000000..3abfb2230b --- /dev/null +++ b/framework/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/EntityFrameworkCore/AbpQueryCompiler.cs @@ -0,0 +1,61 @@ +using System; +using System.Linq.Expressions; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Diagnostics; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Metadata; +using Microsoft.EntityFrameworkCore.Query; +using Microsoft.EntityFrameworkCore.Query.Internal; +using Microsoft.EntityFrameworkCore.Storage; +using Volo.Abp.EntityFrameworkCore.GlobalFilters; + +namespace Volo.Abp.EntityFrameworkCore; + +#pragma warning disable EF1001 +public class AbpQueryCompiler : QueryCompiler +{ + protected AbpEfCoreCurrentDbContext AbpEfCoreCurrentDbContext { get; } + + public AbpQueryCompiler( + IQueryContextFactory queryContextFactory, + ICompiledQueryCache compiledQueryCache, + ICompiledQueryCacheKeyGenerator compiledQueryCacheKeyGenerator, + IDatabase database, + IDiagnosticsLogger logger, + ICurrentDbContext currentContext, + IEvaluatableExpressionFilter evaluatableExpressionFilter, + IModel model, + AbpEfCoreCurrentDbContext abpEfCoreCurrentDbContext) + : base( + queryContextFactory, + compiledQueryCache, + compiledQueryCacheKeyGenerator, + database, + logger, + currentContext, + evaluatableExpressionFilter, + model) + { + AbpEfCoreCurrentDbContext = abpEfCoreCurrentDbContext; + } + + // EF.CompileQuery translates the query once and reuses it for every later run, so the filter state must never be read for it. + // The first run can happen while an outer query is being executed, for example when the compiled query is invoked inside its expression, + // so the current DbContext of that outer query is cleared here. + public override Func CreateCompiledQuery(Expression query) + { + using (AbpEfCoreCurrentDbContext.Use(null)) + { + return base.CreateCompiledQuery(query); + } + } + + public override Func CreateCompiledAsyncQuery(Expression query) + { + using (AbpEfCoreCurrentDbContext.Use(null)) + { + return base.CreateCompiledAsyncQuery(query); + } + } +} +#pragma warning restore EF1001 diff --git a/framework/test/Volo.Abp.EntityFrameworkCore.Tests/Volo/Abp/EntityFrameworkCore/DataFiltering/CompiledQueryCache/CompiledQueryCacheMultiTenantRow.cs b/framework/test/Volo.Abp.EntityFrameworkCore.Tests/Volo/Abp/EntityFrameworkCore/DataFiltering/CompiledQueryCache/CompiledQueryCacheMultiTenantRow.cs new file mode 100644 index 0000000000..c80b5e4f0b --- /dev/null +++ b/framework/test/Volo.Abp.EntityFrameworkCore.Tests/Volo/Abp/EntityFrameworkCore/DataFiltering/CompiledQueryCache/CompiledQueryCacheMultiTenantRow.cs @@ -0,0 +1,11 @@ +using System; +using Volo.Abp.Domain.Entities; +using Volo.Abp.MultiTenancy; + +namespace Volo.Abp.EntityFrameworkCore.DataFiltering.CompiledQueryCache; + +public class CompiledQueryCacheMultiTenantRow : Entity, IMultiTenant +{ + public Guid? TenantId { get; set; } + public string Name { get; set; } +} diff --git a/framework/test/Volo.Abp.EntityFrameworkCore.Tests/Volo/Abp/EntityFrameworkCore/DataFiltering/CompiledQueryCache/CompiledQueryCacheSoftDeleteRow.cs b/framework/test/Volo.Abp.EntityFrameworkCore.Tests/Volo/Abp/EntityFrameworkCore/DataFiltering/CompiledQueryCache/CompiledQueryCacheSoftDeleteRow.cs new file mode 100644 index 0000000000..52ed2591ae --- /dev/null +++ b/framework/test/Volo.Abp.EntityFrameworkCore.Tests/Volo/Abp/EntityFrameworkCore/DataFiltering/CompiledQueryCache/CompiledQueryCacheSoftDeleteRow.cs @@ -0,0 +1,11 @@ +using System; +using Volo.Abp.Data; +using Volo.Abp.Domain.Entities; + +namespace Volo.Abp.EntityFrameworkCore.DataFiltering.CompiledQueryCache; + +public class CompiledQueryCacheSoftDeleteRow : Entity, ISoftDelete +{ + public bool IsDeleted { get; set; } + public string Name { get; set; } +} diff --git a/framework/test/Volo.Abp.EntityFrameworkCore.Tests/Volo/Abp/EntityFrameworkCore/DataFiltering/CompiledQueryCache/CompiledQueryCacheTestBase.cs b/framework/test/Volo.Abp.EntityFrameworkCore.Tests/Volo/Abp/EntityFrameworkCore/DataFiltering/CompiledQueryCache/CompiledQueryCacheTestBase.cs index 3e901dd795..c8de61e946 100644 --- a/framework/test/Volo.Abp.EntityFrameworkCore.Tests/Volo/Abp/EntityFrameworkCore/DataFiltering/CompiledQueryCache/CompiledQueryCacheTestBase.cs +++ b/framework/test/Volo.Abp.EntityFrameworkCore.Tests/Volo/Abp/EntityFrameworkCore/DataFiltering/CompiledQueryCache/CompiledQueryCacheTestBase.cs @@ -154,4 +154,29 @@ public abstract class CompiledQueryCacheTestBase : TestAppTestBa (counter.Count - before).ShouldBe(1); await uow.CompleteAsync(); } + + protected async Task> RunInTenantAsync( + Guid? tenantId, + Func>> query) + { + using var uow = GetRequiredService().Begin(); + using var tenant = GetRequiredService().Change(tenantId); + var context = GetRequiredService(); + context.UseDbFunction().ShouldBe(UseDbFunction); + + var names = await query(context); + await uow.CompleteAsync(); + return names; + } + + protected static async Task> ToListAsync(IAsyncEnumerable source) + { + var names = new List(); + await foreach (var name in source) + { + names.Add(name); + } + + return names; + } } diff --git a/framework/test/Volo.Abp.EntityFrameworkCore.Tests/Volo/Abp/EntityFrameworkCore/DataFiltering/CompiledQueryCache/CompiledQueryCacheTestDbContext.cs b/framework/test/Volo.Abp.EntityFrameworkCore.Tests/Volo/Abp/EntityFrameworkCore/DataFiltering/CompiledQueryCache/CompiledQueryCacheTestDbContext.cs index 5ccd9f0119..b22ebc1aac 100644 --- a/framework/test/Volo.Abp.EntityFrameworkCore.Tests/Volo/Abp/EntityFrameworkCore/DataFiltering/CompiledQueryCache/CompiledQueryCacheTestDbContext.cs +++ b/framework/test/Volo.Abp.EntityFrameworkCore.Tests/Volo/Abp/EntityFrameworkCore/DataFiltering/CompiledQueryCache/CompiledQueryCacheTestDbContext.cs @@ -6,6 +6,10 @@ public class CompiledQueryCacheTestDbContext : AbpDbContext Rows { get; set; } + public DbSet SoftDeleteRows { get; set; } + + public DbSet MultiTenantRows { get; set; } + public CompiledQueryCacheTestDbContext(DbContextOptions options) : base(options) { diff --git a/framework/test/Volo.Abp.EntityFrameworkCore.Tests/Volo/Abp/EntityFrameworkCore/DataFiltering/CompiledQueryCache/CompiledQueryCache_Tests.cs b/framework/test/Volo.Abp.EntityFrameworkCore.Tests/Volo/Abp/EntityFrameworkCore/DataFiltering/CompiledQueryCache/CompiledQueryCache_Tests.cs index 299fd8b58d..059123395d 100644 --- a/framework/test/Volo.Abp.EntityFrameworkCore.Tests/Volo/Abp/EntityFrameworkCore/DataFiltering/CompiledQueryCache/CompiledQueryCache_Tests.cs +++ b/framework/test/Volo.Abp.EntityFrameworkCore.Tests/Volo/Abp/EntityFrameworkCore/DataFiltering/CompiledQueryCache/CompiledQueryCache_Tests.cs @@ -1,6 +1,58 @@ +using System; +using System.Linq; +using System.Threading.Tasks; +using Microsoft.EntityFrameworkCore; +using Shouldly; +using Xunit; + namespace Volo.Abp.EntityFrameworkCore.DataFiltering.CompiledQueryCache; public class CompiledQueryCache_Tests : CompiledQueryCacheTestBase { protected override bool UseDbFunction => true; + + // EF.CompileQuery translates the query outside of AbpEntityQueryProvider, so the filter state is not available. + // Each filter is tested on its own entity, so one translation cannot hide a missing check in the other. + [Fact] + public async Task Should_Not_Run_Compiled_Query_Without_SoftDelete_Filter() + { + var query = EF.CompileQuery((CompiledQueryCacheTestDbContext db) => db.SoftDeleteRows.Select(x => x.Name)); + + await Should.ThrowAsync(() => + RunInTenantAsync(CompiledQueryCacheTestModuleBase.TenantA, db => Task.FromResult(query(db).ToList()))); + } + + [Fact] + public async Task Should_Not_Run_Compiled_Query_Without_MultiTenant_Filter() + { + var query = EF.CompileQuery((CompiledQueryCacheTestDbContext db) => db.MultiTenantRows.Select(x => x.Name)); + + await Should.ThrowAsync(() => + RunInTenantAsync(CompiledQueryCacheTestModuleBase.TenantA, db => Task.FromResult(query(db).ToList()))); + } + + // The compiled query is first run while the outer query is executed, so the current DbContext of the outer query is set. + [Fact] + public async Task Should_Not_Run_Compiled_Query_First_Run_Inside_Another_Query_Without_Filters() + { + var countRows = EF.CompileQuery((CompiledQueryCacheTestDbContext db) => db.Rows.Count()); + + // EF Core wraps the exceptions thrown while it evaluates the parameters of the outer query. + var exception = await Should.ThrowAsync(() => + RunInTenantAsync(CompiledQueryCacheTestModuleBase.TenantA, db => + Task.FromResult(db.Rows.Where(row => row.Name.Length > countRows(db)).Select(row => row.Name).ToList()))); + exception.InnerException.ShouldBeOfType(); + + await Should.ThrowAsync(() => + RunInTenantAsync(CompiledQueryCacheTestModuleBase.TenantA, db => Task.FromResult(new[] { countRows(db).ToString() }.ToList()))); + } + + [Fact] + public async Task Should_Not_Run_Compiled_Async_Query_Without_Filters() + { + var query = EF.CompileAsyncQuery((CompiledQueryCacheTestDbContext db) => db.Rows.Select(x => x.Name)); + + await Should.ThrowAsync(() => + RunInTenantAsync(CompiledQueryCacheTestModuleBase.TenantA, db => ToListAsync(query(db)))); + } } diff --git a/framework/test/Volo.Abp.EntityFrameworkCore.Tests/Volo/Abp/EntityFrameworkCore/DataFiltering/CompiledQueryCache/CompiledQueryCache_WithoutDbFunction_Tests.cs b/framework/test/Volo.Abp.EntityFrameworkCore.Tests/Volo/Abp/EntityFrameworkCore/DataFiltering/CompiledQueryCache/CompiledQueryCache_WithoutDbFunction_Tests.cs index 37b93df924..6fdd232ee6 100644 --- a/framework/test/Volo.Abp.EntityFrameworkCore.Tests/Volo/Abp/EntityFrameworkCore/DataFiltering/CompiledQueryCache/CompiledQueryCache_WithoutDbFunction_Tests.cs +++ b/framework/test/Volo.Abp.EntityFrameworkCore.Tests/Volo/Abp/EntityFrameworkCore/DataFiltering/CompiledQueryCache/CompiledQueryCache_WithoutDbFunction_Tests.cs @@ -1,6 +1,31 @@ +using System.Linq; +using System.Threading.Tasks; +using Microsoft.EntityFrameworkCore; +using Shouldly; +using Xunit; + namespace Volo.Abp.EntityFrameworkCore.DataFiltering.CompiledQueryCache; public class CompiledQueryCache_WithoutDbFunction_Tests : CompiledQueryCacheTestBase { protected override bool UseDbFunction => false; + + // Without DbFunction the filters are parameters of the compiled query, so they are applied on every run. + [Fact] + public async Task Should_Apply_Filters_To_Compiled_Queries() + { + var query = EF.CompileQuery((CompiledQueryCacheTestDbContext db) => + db.Rows.OrderBy(x => x.Name).Select(x => x.Name)); + var asyncQuery = EF.CompileAsyncQuery((CompiledQueryCacheTestDbContext db) => + db.Rows.OrderBy(x => x.Name).Select(x => x.Name)); + + (await RunInTenantAsync(CompiledQueryCacheTestModuleBase.TenantA, db => Task.FromResult(query(db).ToList()))) + .ShouldBe(new[] { "A-live" }); + (await RunInTenantAsync(CompiledQueryCacheTestModuleBase.TenantB, db => Task.FromResult(query(db).ToList()))) + .ShouldBe(new[] { "B-live" }); + (await RunInTenantAsync(null, db => ToListAsync(asyncQuery(db)))) + .ShouldBe(new[] { "Host-live" }); + (await RunInTenantAsync(CompiledQueryCacheTestModuleBase.TenantA, db => ToListAsync(asyncQuery(db)))) + .ShouldBe(new[] { "A-live" }); + } } From 93ad78974b9b86e8ff39f33cc1f44042b0c50e7b Mon Sep 17 00:00:00 2001 From: maliming Date: Wed, 14 Oct 2026 15:13:17 +0800 Subject: [PATCH 2/5] Document compiled query compiler replacement and test async nested compilation --- .../framework/infrastructure/data-filtering.md | 14 +++++++------- .../release-info/migration-guides/abp-10-7.md | 5 ++++- .../CompiledQueryCache_Tests.cs | 17 +++++++++++++++++ 3 files changed, 28 insertions(+), 8 deletions(-) diff --git a/docs/en/framework/infrastructure/data-filtering.md b/docs/en/framework/infrastructure/data-filtering.md index e0b23bcb70..995d5d4223 100644 --- a/docs/en/framework/infrastructure/data-filtering.md +++ b/docs/en/framework/infrastructure/data-filtering.md @@ -224,9 +224,9 @@ protected override bool ShouldFilterEntity(IMutableEntityType entityTyp return base.ShouldFilterEntity(entityType); } -protected override Expression> CreateFilterExpression(ModelBuilder modelBuilder) +protected override Expression> CreateFilterExpression(ModelBuilder modelBuilder, EntityTypeBuilder entityTypeBuilder) { - var expression = base.CreateFilterExpression(modelBuilder); + var expression = base.CreateFilterExpression(modelBuilder, entityTypeBuilder); if (typeof(IIsActive).IsAssignableFrom(typeof(TEntity))) { @@ -274,9 +274,9 @@ protected override bool ShouldFilterEntity(IMutableEntityType entityTyp return base.ShouldFilterEntity(entityType); } -protected override Expression> CreateFilterExpression(ModelBuilder modelBuilder) +protected override Expression> CreateFilterExpression(ModelBuilder modelBuilder, EntityTypeBuilder entityTypeBuilder) { - var expression = base.CreateFilterExpression(modelBuilder); + var expression = base.CreateFilterExpression(modelBuilder, entityTypeBuilder); if (typeof(IIsActive).IsAssignableFrom(typeof(TEntity))) { @@ -304,13 +304,13 @@ protected override Expression> CreateFilterExpression()` no longer finds `QueryCompiler`, so use `ReplaceService()`. -> See the [Data Filtering](../../framework/infrastructure/data-filtering.md) document and [#26282](https://github.com/abpframework/abp/pull/26282) for details. +> See the [Data Filtering](../../framework/infrastructure/data-filtering.md) document and [#26284](https://github.com/abpframework/abp/pull/26284) for details. ### Dependency Updates diff --git a/framework/test/Volo.Abp.EntityFrameworkCore.Tests/Volo/Abp/EntityFrameworkCore/DataFiltering/CompiledQueryCache/CompiledQueryCache_Tests.cs b/framework/test/Volo.Abp.EntityFrameworkCore.Tests/Volo/Abp/EntityFrameworkCore/DataFiltering/CompiledQueryCache/CompiledQueryCache_Tests.cs index 059123395d..2d9fe405a2 100644 --- a/framework/test/Volo.Abp.EntityFrameworkCore.Tests/Volo/Abp/EntityFrameworkCore/DataFiltering/CompiledQueryCache/CompiledQueryCache_Tests.cs +++ b/framework/test/Volo.Abp.EntityFrameworkCore.Tests/Volo/Abp/EntityFrameworkCore/DataFiltering/CompiledQueryCache/CompiledQueryCache_Tests.cs @@ -47,6 +47,23 @@ public class CompiledQueryCache_Tests : CompiledQueryCacheTestBase Task.FromResult(new[] { countRows(db).ToString() }.ToList()))); } + [Fact] + public async Task Should_Not_Run_Compiled_Async_Query_First_Run_Inside_Another_Query_Without_Filters() + { + var countRowsAsync = EF.CompileAsyncQuery((CompiledQueryCacheTestDbContext db) => db.Rows.Count()); + + var exception = await Should.ThrowAsync(() => + RunInTenantAsync(CompiledQueryCacheTestModuleBase.TenantA, db => + Task.FromResult(db.Rows + .Where(row => row.Name.Length > countRowsAsync(db).GetAwaiter().GetResult()) + .Select(row => row.Name) + .ToList()))); + exception.InnerException.ShouldBeOfType(); + + await Should.ThrowAsync(() => + RunInTenantAsync(CompiledQueryCacheTestModuleBase.TenantA, async db => new[] { (await countRowsAsync(db)).ToString() }.ToList())); + } + [Fact] public async Task Should_Not_Run_Compiled_Async_Query_Without_Filters() { From 6ee3b2ed5a7093b2f7d3ef21462a6933a08e7110 Mon Sep 17 00:00:00 2001 From: maliming Date: Wed, 14 Oct 2026 15:31:01 +0800 Subject: [PATCH 3/5] Reuse the current DbContext check through GetRequiredContext --- docs/en/framework/infrastructure/data-filtering.md | 9 +++------ docs/en/release-info/migration-guides/abp-10-7.md | 2 +- .../AbpEfCoreModelBuilderExtensions.cs | 13 ++----------- .../GlobalFilters/AbpEfCoreCurrentDbContext.cs | 9 +++++++++ 4 files changed, 15 insertions(+), 18 deletions(-) diff --git a/docs/en/framework/infrastructure/data-filtering.md b/docs/en/framework/infrastructure/data-filtering.md index 995d5d4223..eff7b12960 100644 --- a/docs/en/framework/infrastructure/data-filtering.md +++ b/docs/en/framework/infrastructure/data-filtering.md @@ -294,11 +294,8 @@ protected override Expression> CreateFilterExpression()) + // Throws when there is no current DbContext, instead of running the query without the filter. + if (abpEfCoreCurrentDbContext.GetRequiredContext().DataFilter.IsEnabled()) { // isActive == true return new SqlBinaryExpression( @@ -350,7 +347,7 @@ Include any other values read at compilation time in the cache key as well. Keep The filter state is read while EF Core translates the query. ABP can only provide it for queries that run through `AbpEntityQueryProvider`, such as normal LINQ queries. -Compiled queries (`EF.CompileQuery` and `EF.CompileAsyncQuery`) are always translated without it, even when they first run inside another query. So if a compiled query needs one of these filters, ABP throws an `AbpException` instead of running it without the filter. Compiled queries on entities without these filters, or with `IgnoreQueryFilters()`, work as before. Run such queries as normal LINQ queries, or set `UseDbFunction` of `AbpEfCoreGlobalFilterOptions` to `false`. Your custom `HasTranslation` callbacks should also throw when `AbpEfCoreCurrentDbContext.Context` is `null`, as the example above does. +Compiled queries (`EF.CompileQuery` and `EF.CompileAsyncQuery`) are always translated without it, even when they first run inside another query. So if a compiled query needs one of these filters, ABP throws an `AbpException` instead of running it without the filter. Compiled queries on entities without these filters, or with `IgnoreQueryFilters()`, work as before. Run such queries as normal LINQ queries, or set `UseDbFunction` of `AbpEfCoreGlobalFilterOptions` to `false`. Your custom `HasTranslation` callbacks should read the filter state through `AbpEfCoreCurrentDbContext.GetRequiredContext()`, as the example above does, so they throw in the same case. ##### Make User-defined function mapping compatible with [DevExtreme.AspNet.Data's](https://github.com/DevExpress/DevExtreme.AspNet.Data) `IAsyncAdapter` diff --git a/docs/en/release-info/migration-guides/abp-10-7.md b/docs/en/release-info/migration-guides/abp-10-7.md index 2196965d58..fe6c43a9e9 100644 --- a/docs/en/release-info/migration-guides/abp-10-7.md +++ b/docs/en/release-info/migration-guides/abp-10-7.md @@ -272,7 +272,7 @@ Add any other value the callback reads at compilation time to the key the same w **What to do** - Run the compiled queries as normal LINQ queries, or set `UseDbFunction` of `AbpEfCoreGlobalFilterOptions` to `false`. -- Make your custom `HasTranslation` callbacks throw when `AbpEfCoreCurrentDbContext.Context` is `null`, instead of treating it as a disabled filter. +- Read the filter state in your custom `HasTranslation` callbacks through the new `AbpEfCoreCurrentDbContext.GetRequiredContext()` method. It throws when there is no current DbContext, instead of treating it as a disabled filter. - If you replace `IQueryCompiler`, derive your implementation from `AbpQueryCompiler`. Otherwise it removes the check above for compiled queries that first run inside another query. `ReplaceService()` no longer finds `QueryCompiler`, so use `ReplaceService()`. > See the [Data Filtering](../../framework/infrastructure/data-filtering.md) document and [#26284](https://github.com/abpframework/abp/pull/26284) for details. diff --git a/framework/src/Volo.Abp.EntityFrameworkCore/Microsoft/Extensions/DependencyInjection/AbpEfCoreModelBuilderExtensions.cs b/framework/src/Volo.Abp.EntityFrameworkCore/Microsoft/Extensions/DependencyInjection/AbpEfCoreModelBuilderExtensions.cs index 6fccd33502..9c5626af4f 100644 --- a/framework/src/Volo.Abp.EntityFrameworkCore/Microsoft/Extensions/DependencyInjection/AbpEfCoreModelBuilderExtensions.cs +++ b/framework/src/Volo.Abp.EntityFrameworkCore/Microsoft/Extensions/DependencyInjection/AbpEfCoreModelBuilderExtensions.cs @@ -19,7 +19,7 @@ public static class AbpEfCoreModelBuilderExtensions var isDeleted = args[0]; var boolParam = args[1]; - if (GetCurrentDbContext(abpEfCoreCurrentDbContext).DataFilter.IsEnabled()) + if (abpEfCoreCurrentDbContext.GetRequiredContext().DataFilter.IsEnabled()) { // IsDeleted == false. Use the column's TypeMapping so any ValueConverter is applied to the literal. return new SqlBinaryExpression( @@ -47,7 +47,7 @@ public static class AbpEfCoreModelBuilderExtensions var currentTenantId = args[1]; var boolParam = args[2]; - if (GetCurrentDbContext(abpEfCoreCurrentDbContext).DataFilter.IsEnabled()) + if (abpEfCoreCurrentDbContext.GetRequiredContext().DataFilter.IsEnabled()) { // TenantId == CurrentTenantId return new SqlBinaryExpression( @@ -64,13 +64,4 @@ public static class AbpEfCoreModelBuilderExtensions return modelBuilder; } - - // The filter state is read while EF Core translates the query, and only AbpEntityQueryProvider sets the current DbContext for that. - // Queries compiled without it, such as EF.CompileQuery, must fail instead of running without the data filters. - private static IAbpEfCoreDbFunctionContext GetCurrentDbContext(AbpEfCoreCurrentDbContext abpEfCoreCurrentDbContext) - { - return abpEfCoreCurrentDbContext.Context ?? throw new AbpException( - "The data filters cannot be applied to a query that is compiled outside of AbpEntityQueryProvider, such as EF.CompileQuery or EF.CompileAsyncQuery. " + - "Run it as a normal LINQ query, or set 'UseDbFunction' of 'AbpEfCoreGlobalFilterOptions' to false."); - } } diff --git a/framework/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/EntityFrameworkCore/GlobalFilters/AbpEfCoreCurrentDbContext.cs b/framework/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/EntityFrameworkCore/GlobalFilters/AbpEfCoreCurrentDbContext.cs index c3a29b6d70..e0a32417aa 100644 --- a/framework/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/EntityFrameworkCore/GlobalFilters/AbpEfCoreCurrentDbContext.cs +++ b/framework/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/EntityFrameworkCore/GlobalFilters/AbpEfCoreCurrentDbContext.cs @@ -9,6 +9,15 @@ public class AbpEfCoreCurrentDbContext public IAbpEfCoreDbFunctionContext? Context => _current.Value; + // The filter state is read while EF Core translates the query, and only AbpEntityQueryProvider sets the current DbContext for that. + // Queries compiled without it, such as EF.CompileQuery, must fail instead of running without the data filters. + public virtual IAbpEfCoreDbFunctionContext GetRequiredContext() + { + return Context ?? throw new AbpException( + "The data filters cannot be applied to a query that is compiled outside of AbpEntityQueryProvider, such as EF.CompileQuery or EF.CompileAsyncQuery. " + + "Run it as a normal LINQ query, or set 'UseDbFunction' of 'AbpEfCoreGlobalFilterOptions' to false."); + } + public IDisposable Use(IAbpEfCoreDbFunctionContext? context) { var previousValue = Context; From 8c065abd736ed3ff5b64fbbb0a6f9ed9cf6ef358 Mon Sep 17 00:00:00 2001 From: maliming Date: Thu, 15 Oct 2026 10:59:51 +0800 Subject: [PATCH 4/5] Wrap IDatabase instead of subclassing QueryCompiler --- .../POST.md | 7 +- .../infrastructure/data-filtering.md | 24 +- .../release-info/migration-guides/abp-10-7.md | 17 +- .../AbpDbContextOptionsExtension.cs | 32 +-- .../AbpEntityQueryProvider.cs | 34 ++- .../EntityFrameworkCore/AbpQueryCompiler.cs | 61 ----- .../ServiceCollectionDecorateExtensions.cs | 81 +++++++ .../AbpCompiledQueryCacheKeyGenerator.cs | 2 + .../AbpEfCoreCurrentDbContext.cs | 53 ++++- .../GlobalFilters/AbpEfCoreDatabase.cs | 56 +++++ .../AbpDbContextOptionsExtension_Tests.cs | 77 +++++++ .../CompiledQueryCacheTestBase.cs | 21 ++ .../CompiledQueryCacheTestDatabase.cs | 46 ++++ .../CompiledQueryCacheTestHooks.cs | 49 ++++ .../CompiledQueryCacheTestModuleBase.cs | 59 ++++- ...ueryCacheTestModuleWithAbpServicesFirst.cs | 13 ++ ...QueryCacheTestModuleWithDatabaseFactory.cs | 13 ++ ...edQueryCacheTestModuleWithQueryCompiler.cs | 43 ++++ .../CompiledQueryCacheTestQueryCompiler.cs | 56 +++++ ...mpiledQueryCache_AbpServicesFirst_Tests.cs | 25 ++ ...ompiledQueryCache_DatabaseFactory_Tests.cs | 49 ++++ .../CompiledQueryCache_QueryCompiler_Tests.cs | 75 ++++++ .../CompiledQueryCache_Tests.cs | 214 +++++++++++++++++- ...piledQueryCache_WithoutDbFunction_Tests.cs | 29 ++- ...rviceCollectionDecorateExtensions_Tests.cs | 160 +++++++++++++ 25 files changed, 1189 insertions(+), 107 deletions(-) delete mode 100644 framework/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/EntityFrameworkCore/AbpQueryCompiler.cs create mode 100644 framework/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/EntityFrameworkCore/DependencyInjection/ServiceCollectionDecorateExtensions.cs create mode 100644 framework/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/EntityFrameworkCore/GlobalFilters/AbpEfCoreDatabase.cs create mode 100644 framework/test/Volo.Abp.EntityFrameworkCore.Tests/Volo/Abp/EntityFrameworkCore/DataFiltering/CompiledQueryCache/AbpDbContextOptionsExtension_Tests.cs create mode 100644 framework/test/Volo.Abp.EntityFrameworkCore.Tests/Volo/Abp/EntityFrameworkCore/DataFiltering/CompiledQueryCache/CompiledQueryCacheTestDatabase.cs create mode 100644 framework/test/Volo.Abp.EntityFrameworkCore.Tests/Volo/Abp/EntityFrameworkCore/DataFiltering/CompiledQueryCache/CompiledQueryCacheTestHooks.cs create mode 100644 framework/test/Volo.Abp.EntityFrameworkCore.Tests/Volo/Abp/EntityFrameworkCore/DataFiltering/CompiledQueryCache/CompiledQueryCacheTestModuleWithAbpServicesFirst.cs create mode 100644 framework/test/Volo.Abp.EntityFrameworkCore.Tests/Volo/Abp/EntityFrameworkCore/DataFiltering/CompiledQueryCache/CompiledQueryCacheTestModuleWithDatabaseFactory.cs create mode 100644 framework/test/Volo.Abp.EntityFrameworkCore.Tests/Volo/Abp/EntityFrameworkCore/DataFiltering/CompiledQueryCache/CompiledQueryCacheTestModuleWithQueryCompiler.cs create mode 100644 framework/test/Volo.Abp.EntityFrameworkCore.Tests/Volo/Abp/EntityFrameworkCore/DataFiltering/CompiledQueryCache/CompiledQueryCacheTestQueryCompiler.cs create mode 100644 framework/test/Volo.Abp.EntityFrameworkCore.Tests/Volo/Abp/EntityFrameworkCore/DataFiltering/CompiledQueryCache/CompiledQueryCache_AbpServicesFirst_Tests.cs create mode 100644 framework/test/Volo.Abp.EntityFrameworkCore.Tests/Volo/Abp/EntityFrameworkCore/DataFiltering/CompiledQueryCache/CompiledQueryCache_DatabaseFactory_Tests.cs create mode 100644 framework/test/Volo.Abp.EntityFrameworkCore.Tests/Volo/Abp/EntityFrameworkCore/DataFiltering/CompiledQueryCache/CompiledQueryCache_QueryCompiler_Tests.cs create mode 100644 framework/test/Volo.Abp.EntityFrameworkCore.Tests/Volo/Abp/EntityFrameworkCore/DependencyInjection/ServiceCollectionDecorateExtensions_Tests.cs diff --git a/docs/en/Community-Articles/2024-07-01-Use_User-Defined_Function_Mapping_For_Global_Filter/POST.md b/docs/en/Community-Articles/2024-07-01-Use_User-Defined_Function_Mapping_For_Global_Filter/POST.md index 37171d1219..12f9105125 100644 --- a/docs/en/Community-Articles/2024-07-01-Use_User-Defined_Function_Mapping_For_Global_Filter/POST.md +++ b/docs/en/Community-Articles/2024-07-01-Use_User-Defined_Function_Mapping_For_Global_Filter/POST.md @@ -69,13 +69,14 @@ protected override Expression> CreateFilterExpression() == true) + var dbContext = abpEfCoreCurrentDbContext.Context ?? throw new InvalidOperationException("The IIsActive filter cannot be applied to this query."); + if (dbContext.DataFilter.IsEnabled()) { // isActive == true return new SqlBinaryExpression( ExpressionType.Equal, isActive, - new SqlConstantExpression(Expression.Constant(true), boolParam.TypeMapping), + new SqlConstantExpression(Expression.Constant(true), isActive.TypeMapping ?? boolParam.TypeMapping), boolParam.Type, boolParam.TypeMapping); } @@ -102,6 +103,8 @@ public override string GetCompiledQueryCacheKey() } ```` +> Throw an exception when `abpEfCoreCurrentDbContext.Context` is `null` instead of treating the filter as disabled. In the latest ABP versions, use `GetRequiredContext()`. See the [Data Filtering](https://abp.io/docs/latest/framework/infrastructure/data-filtering) document for the latest example and the limitations of compiled queries. + After these changes, the SQL generated by the EF Core Global Query Filters will be as follows: Enabling the `IIsActive` filter: diff --git a/docs/en/framework/infrastructure/data-filtering.md b/docs/en/framework/infrastructure/data-filtering.md index eff7b12960..9657999bca 100644 --- a/docs/en/framework/infrastructure/data-filtering.md +++ b/docs/en/framework/infrastructure/data-filtering.md @@ -294,14 +294,13 @@ protected override Expression> CreateFilterExpression()) { // isActive == true return new SqlBinaryExpression( ExpressionType.Equal, isActive, - new SqlConstantExpression(true, typeof(bool), boolParam.TypeMapping), + new SqlConstantExpression(true, typeof(bool), isActive.TypeMapping ?? boolParam.TypeMapping), boolParam.Type, boolParam.TypeMapping); } @@ -343,11 +342,26 @@ public override string GetCompiledQueryCacheKey() Include any other values read at compilation time in the cache key as well. Keep the call to `base.GetCompiledQueryCacheKey()` to preserve ABP's filter-state keys. -##### Limitations of User-defined function mapping +##### Compiled queries -The filter state is read while EF Core translates the query. ABP can only provide it for queries that run through `AbpEntityQueryProvider`, such as normal LINQ queries. +When user-defined function mapping is enabled, `EF.CompileQuery` and `EF.CompileAsyncQuery` cannot use the soft delete and multi-tenant filters, or your own filters that call `AbpEfCoreCurrentDbContext.GetRequiredContext()`. Queries that need these filters throw an `AbpException`. Queries that don't need them, for example with `IgnoreQueryFilters()`, are not affected. -Compiled queries (`EF.CompileQuery` and `EF.CompileAsyncQuery`) are always translated without it, even when they first run inside another query. So if a compiled query needs one of these filters, ABP throws an `AbpException` instead of running it without the filter. Compiled queries on entities without these filters, or with `IgnoreQueryFilters()`, work as before. Run such queries as normal LINQ queries, or set `UseDbFunction` of `AbpEfCoreGlobalFilterOptions` to `false`. Your custom `HasTranslation` callbacks should read the filter state through `AbpEfCoreCurrentDbContext.GetRequiredContext()`, as the example above does, so they throw in the same case. +Run these queries as normal LINQ queries, or disable user-defined function mapping: + +````csharp +Configure(options => +{ + options.UseDbFunction = false; +}); +```` + +The data filters still apply when `UseDbFunction` is `false`. Use this setting if a third-party library compiles filtered queries itself. + +In custom `HasTranslation` callbacks, use `AbpEfCoreCurrentDbContext.GetRequiredContext()` instead of `Context`, as shown above. + +Don't replace `ICompiledQueryCacheKeyGenerator` or `IDatabase` with `ReplaceService` while user-defined function mapping is enabled. If you need to replace either service, set `UseDbFunction` to `false` and don't add `AbpDbContextOptionsExtension` yourself. + +If you build EF Core's internal service provider yourself, call `new AbpDbContextOptionsExtension().ApplyServices(services)` after registering the database provider services. ##### Make User-defined function mapping compatible with [DevExtreme.AspNet.Data's](https://github.com/DevExpress/DevExtreme.AspNet.Data) `IAsyncAdapter` diff --git a/docs/en/release-info/migration-guides/abp-10-7.md b/docs/en/release-info/migration-guides/abp-10-7.md index fe6c43a9e9..7665cadca4 100644 --- a/docs/en/release-info/migration-guides/abp-10-7.md +++ b/docs/en/release-info/migration-guides/abp-10-7.md @@ -259,21 +259,22 @@ Add any other value the callback reads at compilation time to the key the same w **Who is affected** -- Applications that use `EF.CompileQuery` or `EF.CompileAsyncQuery` for entities that implement `ISoftDelete` or `IMultiTenant`, while `AbpEfCoreGlobalFilterOptions.UseDbFunction` is enabled. It is enabled by default for the SQL Server, MySQL, PostgreSQL, Oracle and SQLite providers. +- Applications that use `EF.CompileQuery` or `EF.CompileAsyncQuery` on entities with the soft delete or multi-tenant filters, while `AbpEfCoreGlobalFilterOptions.UseDbFunction` is enabled. It is enabled by default for the SQL Server, MySQL, PostgreSQL, Oracle and SQLite providers. +- Applications that use third-party libraries that compile filtered queries themselves. - Applications with custom `HasTranslation` filters that read `AbpEfCoreCurrentDbContext.Context`. -- Applications that replace EF Core's `IQueryCompiler` service while `UseDbFunction` is enabled. +- Applications that replace `ICompiledQueryCacheKeyGenerator` or `IDatabase` with `ReplaceService` while `UseDbFunction` is enabled. **What changed** -- The built-in filters read the filter state from `AbpEfCoreCurrentDbContext` while EF Core translates the query, and only `AbpEntityQueryProvider` sets it. A compiled query is translated without it, and the filters were treated as disabled. Such a query returned soft deleted rows and the rows of other tenants. -- The soft delete and multi-tenant filters now throw an `AbpException` for such a query instead of running it without the filters, even when the compiled query first runs inside another query. -- To support the second case, ABP replaces EF Core's `IQueryCompiler` with `AbpQueryCompiler`. It clears the current DbContext while a compiled query is created. +- These queries could run without the soft delete and multi-tenant filters and return soft deleted rows and the rows of other tenants. They now throw an `AbpException`. +- Replacing `ICompiledQueryCacheKeyGenerator` or `IDatabase` with `ReplaceService` now throws an `AbpException`. **What to do** -- Run the compiled queries as normal LINQ queries, or set `UseDbFunction` of `AbpEfCoreGlobalFilterOptions` to `false`. -- Read the filter state in your custom `HasTranslation` callbacks through the new `AbpEfCoreCurrentDbContext.GetRequiredContext()` method. It throws when there is no current DbContext, instead of treating it as a disabled filter. -- If you replace `IQueryCompiler`, derive your implementation from `AbpQueryCompiler`. Otherwise it removes the check above for compiled queries that first run inside another query. `ReplaceService()` no longer finds `QueryCompiler`, so use `ReplaceService()`. +- Run filtered queries as normal LINQ queries, or set `UseDbFunction` to `false`. The data filters still apply with this setting. +- Use the new `AbpEfCoreCurrentDbContext.GetRequiredContext()` method instead of `Context` in custom `HasTranslation` callbacks. +- If you need to replace `ICompiledQueryCacheKeyGenerator` or `IDatabase`, set `UseDbFunction` to `false` and don't add `AbpDbContextOptionsExtension` yourself. +- If you build EF Core's internal service provider yourself, call `new AbpDbContextOptionsExtension().ApplyServices(services)` after registering the database provider services. > See the [Data Filtering](../../framework/infrastructure/data-filtering.md) document and [#26284](https://github.com/abpframework/abp/pull/26284) for details. diff --git a/framework/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/EntityFrameworkCore/AbpDbContextOptionsExtension.cs b/framework/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/EntityFrameworkCore/AbpDbContextOptionsExtension.cs index 5fdb4fe817..17b0e5dfdb 100644 --- a/framework/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/EntityFrameworkCore/AbpDbContextOptionsExtension.cs +++ b/framework/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/EntityFrameworkCore/AbpDbContextOptionsExtension.cs @@ -3,9 +3,10 @@ using System.Collections.Generic; using System.Linq; using Microsoft.EntityFrameworkCore.Infrastructure; using Microsoft.EntityFrameworkCore.Query; -using Microsoft.EntityFrameworkCore.Query.Internal; +using Microsoft.EntityFrameworkCore.Storage; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection.Extensions; +using Volo.Abp.EntityFrameworkCore.DependencyInjection; using Volo.Abp.EntityFrameworkCore.GlobalFilters; namespace Volo.Abp.EntityFrameworkCore; @@ -14,26 +15,31 @@ public class AbpDbContextOptionsExtension : IDbContextOptionsExtension { public void ApplyServices(IServiceCollection services) { - var serviceDescriptor = services.FirstOrDefault(x => x.ServiceType == typeof(ICompiledQueryCacheKeyGenerator)); - if (serviceDescriptor != null && serviceDescriptor.ImplementationType != null) + services.Decorate((provider, inner) => + ActivatorUtilities.CreateInstance(provider, inner)); + + // The user-defined functions are only translated by relational providers, and others, such as InMemory, cast IDatabase to their own type. + if (services.IsAdded()) { - services.Remove(serviceDescriptor); - services.AddScoped(serviceDescriptor.ImplementationType); - services.Add(ServiceDescriptor.Scoped(provider => - ActivatorUtilities.CreateInstance(provider, - provider.GetRequiredService(serviceDescriptor.ImplementationType) - .As()))); + services.Decorate((provider, inner) => + ActivatorUtilities.CreateInstance(provider, inner)); } services.Replace(ServiceDescriptor.Scoped()); -#pragma warning disable EF1001 - services.Replace(ServiceDescriptor.Scoped()); -#pragma warning restore EF1001 - services.AddSingleton(typeof(AbpEfCoreCurrentDbContext)); + services.TryAddSingleton(); } public void Validate(IDbContextOptions options) { + // EF Core applies ReplaceService after ApplyServices, so it would silently remove the wrappers. + var replacedServices = options.FindExtension()?.ReplacedServices; + if (replacedServices != null && replacedServices.Keys.Any(x => x.Item1 == typeof(ICompiledQueryCacheKeyGenerator) || x.Item1 == typeof(IDatabase))) + { + throw new AbpException( + "ICompiledQueryCacheKeyGenerator and IDatabase cannot be replaced with ReplaceService while AbpDbContextOptionsExtension is used, " + + "because ABP wraps them to apply the data filters. Remove the ReplaceService call, or set 'UseDbFunction' of 'AbpEfCoreGlobalFilterOptions' to false " + + "and don't add AbpDbContextOptionsExtension yourself."); + } } public DbContextOptionsExtensionInfo Info => new AbpOptionsExtensionInfo(this); diff --git a/framework/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/EntityFrameworkCore/AbpEntityQueryProvider.cs b/framework/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/EntityFrameworkCore/AbpEntityQueryProvider.cs index 665757e17b..cfe9079cbf 100644 --- a/framework/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/EntityFrameworkCore/AbpEntityQueryProvider.cs +++ b/framework/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/EntityFrameworkCore/AbpEntityQueryProvider.cs @@ -1,7 +1,10 @@ using System.Linq.Expressions; using System.Threading; +using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Query; using Microsoft.EntityFrameworkCore.Query.Internal; +using Microsoft.EntityFrameworkCore.Storage; using Volo.Abp.EntityFrameworkCore.GlobalFilters; namespace Volo.Abp.EntityFrameworkCore; @@ -12,6 +15,8 @@ public class AbpEntityQueryProvider : EntityQueryProvider protected AbpEfCoreCurrentDbContext AbpEfCoreCurrentDbContext { get; } protected ICurrentDbContext CurrentDbContext { get; } + private bool _queryServicesChecked; + public AbpEntityQueryProvider( IQueryCompiler queryCompiler, AbpEfCoreCurrentDbContext abpEfCoreCurrentDbContext, @@ -24,7 +29,8 @@ public class AbpEntityQueryProvider : EntityQueryProvider public override object Execute(Expression expression) { - using (AbpEfCoreCurrentDbContext.Use(CurrentDbContext.Context as IAbpEfCoreDbFunctionContext)) + CheckQueryServices(); + using (AbpEfCoreCurrentDbContext.BeginQuery(CurrentDbContext.Context as IAbpEfCoreDbFunctionContext)) { return base.Execute(expression); } @@ -32,7 +38,8 @@ public class AbpEntityQueryProvider : EntityQueryProvider public override TResult Execute(Expression expression) { - using (AbpEfCoreCurrentDbContext.Use(CurrentDbContext.Context as IAbpEfCoreDbFunctionContext)) + CheckQueryServices(); + using (AbpEfCoreCurrentDbContext.BeginQuery(CurrentDbContext.Context as IAbpEfCoreDbFunctionContext)) { return base.Execute(expression); } @@ -40,10 +47,31 @@ public class AbpEntityQueryProvider : EntityQueryProvider public override TResult ExecuteAsync(Expression expression, CancellationToken cancellationToken = new CancellationToken()) { - using (AbpEfCoreCurrentDbContext.Use(CurrentDbContext.Context as IAbpEfCoreDbFunctionContext)) + CheckQueryServices(); + using (AbpEfCoreCurrentDbContext.BeginQuery(CurrentDbContext.Context as IAbpEfCoreDbFunctionContext)) { return base.ExecuteAsync(expression, cancellationToken); } } + + protected virtual void CheckQueryServices() + { + if (_queryServicesChecked) + { + return; + } + + var dbContext = CurrentDbContext.Context; + if (dbContext is IAbpEfCoreDbFunctionContext abpDbContext && abpDbContext.UseDbFunction() && dbContext.Database.IsRelational() && + (dbContext.GetService() is not AbpEfCoreDatabase || + dbContext.GetService() is not AbpCompiledQueryCacheKeyGenerator)) + { + throw new AbpException( + "The data filters cannot be applied, because the EF Core services of ABP are not registered. " + + "Add AbpDbContextOptionsExtension after the database provider, or set 'UseDbFunction' of 'AbpEfCoreGlobalFilterOptions' to false."); + } + + _queryServicesChecked = true; + } } #pragma warning restore EF1001 diff --git a/framework/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/EntityFrameworkCore/AbpQueryCompiler.cs b/framework/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/EntityFrameworkCore/AbpQueryCompiler.cs deleted file mode 100644 index 3abfb2230b..0000000000 --- a/framework/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/EntityFrameworkCore/AbpQueryCompiler.cs +++ /dev/null @@ -1,61 +0,0 @@ -using System; -using System.Linq.Expressions; -using Microsoft.EntityFrameworkCore; -using Microsoft.EntityFrameworkCore.Diagnostics; -using Microsoft.EntityFrameworkCore.Infrastructure; -using Microsoft.EntityFrameworkCore.Metadata; -using Microsoft.EntityFrameworkCore.Query; -using Microsoft.EntityFrameworkCore.Query.Internal; -using Microsoft.EntityFrameworkCore.Storage; -using Volo.Abp.EntityFrameworkCore.GlobalFilters; - -namespace Volo.Abp.EntityFrameworkCore; - -#pragma warning disable EF1001 -public class AbpQueryCompiler : QueryCompiler -{ - protected AbpEfCoreCurrentDbContext AbpEfCoreCurrentDbContext { get; } - - public AbpQueryCompiler( - IQueryContextFactory queryContextFactory, - ICompiledQueryCache compiledQueryCache, - ICompiledQueryCacheKeyGenerator compiledQueryCacheKeyGenerator, - IDatabase database, - IDiagnosticsLogger logger, - ICurrentDbContext currentContext, - IEvaluatableExpressionFilter evaluatableExpressionFilter, - IModel model, - AbpEfCoreCurrentDbContext abpEfCoreCurrentDbContext) - : base( - queryContextFactory, - compiledQueryCache, - compiledQueryCacheKeyGenerator, - database, - logger, - currentContext, - evaluatableExpressionFilter, - model) - { - AbpEfCoreCurrentDbContext = abpEfCoreCurrentDbContext; - } - - // EF.CompileQuery translates the query once and reuses it for every later run, so the filter state must never be read for it. - // The first run can happen while an outer query is being executed, for example when the compiled query is invoked inside its expression, - // so the current DbContext of that outer query is cleared here. - public override Func CreateCompiledQuery(Expression query) - { - using (AbpEfCoreCurrentDbContext.Use(null)) - { - return base.CreateCompiledQuery(query); - } - } - - public override Func CreateCompiledAsyncQuery(Expression query) - { - using (AbpEfCoreCurrentDbContext.Use(null)) - { - return base.CreateCompiledAsyncQuery(query); - } - } -} -#pragma warning restore EF1001 diff --git a/framework/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/EntityFrameworkCore/DependencyInjection/ServiceCollectionDecorateExtensions.cs b/framework/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/EntityFrameworkCore/DependencyInjection/ServiceCollectionDecorateExtensions.cs new file mode 100644 index 0000000000..bfb79603d4 --- /dev/null +++ b/framework/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/EntityFrameworkCore/DependencyInjection/ServiceCollectionDecorateExtensions.cs @@ -0,0 +1,81 @@ +using System; +using Microsoft.Extensions.DependencyInjection; + +namespace Volo.Abp.EntityFrameworkCore.DependencyInjection; + +internal static class ServiceCollectionDecorateExtensions +{ + internal static IServiceCollection Decorate( + this IServiceCollection services, + Func decorator) + where TService : class + { + // The container resolves the last registration. + for (var i = services.Count - 1; i >= 0; i--) + { + var descriptor = services[i]; + if (descriptor.ServiceType != typeof(TService) || descriptor.IsKeyedService) + { + continue; + } + + // The same service collection can be decorated more than once, for example when ApplyServices runs again. + if (descriptor.ImplementationFactory?.Target is Decorator) + { + break; + } + + // The original registration stays in the container under its own key, so the container still creates and disposes it. + var innerServiceKey = new object(); + services[i] = new ServiceDescriptor( + typeof(TService), + new Decorator(innerServiceKey, decorator).Create, + descriptor.Lifetime); + services.Add(CreateInnerDescriptor(descriptor, innerServiceKey)); + break; + } + + return services; + } + + private static ServiceDescriptor CreateInnerDescriptor(ServiceDescriptor descriptor, object innerServiceKey) + { + if (descriptor.ImplementationType != null) + { + return new ServiceDescriptor(descriptor.ServiceType, innerServiceKey, descriptor.ImplementationType, descriptor.Lifetime); + } + + if (descriptor.ImplementationFactory != null) + { + var implementationFactory = descriptor.ImplementationFactory; + return new ServiceDescriptor(descriptor.ServiceType, innerServiceKey, (provider, _) => implementationFactory(provider), descriptor.Lifetime); + } + + return new ServiceDescriptor(descriptor.ServiceType, innerServiceKey, descriptor.ImplementationInstance!); + } + + private sealed class Decorator + where TService : class + { + private readonly object _innerServiceKey; + private readonly Func _decorator; + + public Decorator(object innerServiceKey, Func decorator) + { + _innerServiceKey = innerServiceKey; + _decorator = decorator; + } + + public object Create(IServiceProvider serviceProvider) + { + var inner = serviceProvider.GetRequiredKeyedService(_innerServiceKey); + var service = _decorator(serviceProvider, inner); + if (service == null || ReferenceEquals(service, inner)) + { + throw new AbpException($"The decorator of {typeof(TService).FullName} must return a new object."); + } + + return service; + } + } +} diff --git a/framework/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/EntityFrameworkCore/GlobalFilters/AbpCompiledQueryCacheKeyGenerator.cs b/framework/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/EntityFrameworkCore/GlobalFilters/AbpCompiledQueryCacheKeyGenerator.cs index 8ce26b7ce1..45436b49f7 100644 --- a/framework/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/EntityFrameworkCore/GlobalFilters/AbpCompiledQueryCacheKeyGenerator.cs +++ b/framework/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/EntityFrameworkCore/GlobalFilters/AbpCompiledQueryCacheKeyGenerator.cs @@ -39,6 +39,8 @@ public class AbpCompiledQueryCacheKeyGenerator : ICompiledQueryCacheKeyGenerator } } + CurrentContext.Context.GetService().AuthorizeQueryCompilation(abpEfCoreDbFunctionContext, query); + return new AbpCompiledQueryCacheKey(cacheKey, abpCacheKey); } diff --git a/framework/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/EntityFrameworkCore/GlobalFilters/AbpEfCoreCurrentDbContext.cs b/framework/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/EntityFrameworkCore/GlobalFilters/AbpEfCoreCurrentDbContext.cs index e0a32417aa..b397b6aafb 100644 --- a/framework/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/EntityFrameworkCore/GlobalFilters/AbpEfCoreCurrentDbContext.cs +++ b/framework/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/EntityFrameworkCore/GlobalFilters/AbpEfCoreCurrentDbContext.cs @@ -1,4 +1,5 @@ using System; +using System.Linq.Expressions; using System.Threading; namespace Volo.Abp.EntityFrameworkCore.GlobalFilters; @@ -6,15 +7,14 @@ namespace Volo.Abp.EntityFrameworkCore.GlobalFilters; public class AbpEfCoreCurrentDbContext { private readonly AsyncLocal _current = new AsyncLocal(); + private readonly AsyncLocal _queryScope = new AsyncLocal(); public IAbpEfCoreDbFunctionContext? Context => _current.Value; - // The filter state is read while EF Core translates the query, and only AbpEntityQueryProvider sets the current DbContext for that. - // Queries compiled without it, such as EF.CompileQuery, must fail instead of running without the data filters. public virtual IAbpEfCoreDbFunctionContext GetRequiredContext() { return Context ?? throw new AbpException( - "The data filters cannot be applied to a query that is compiled outside of AbpEntityQueryProvider, such as EF.CompileQuery or EF.CompileAsyncQuery. " + + "The data filters cannot be applied to a query that is compiled outside of EF Core's normal query pipeline, such as EF.CompileQuery or EF.CompileAsyncQuery. " + "Run it as a normal LINQ query, or set 'UseDbFunction' of 'AbpEfCoreGlobalFilterOptions' to false."); } @@ -27,4 +27,51 @@ public class AbpEfCoreCurrentDbContext _current.Value = previousValue; }); } + + // Only the query of AbpEntityQueryProvider puts the filter state into the compiled query cache key, so only its compilation + // may read it. Other compilations, such as EF.CompileQuery or a query compiled while another query runs, must fail. + internal IDisposable BeginQuery(IAbpEfCoreDbFunctionContext? context) + { + var previousScope = _queryScope.Value; + _queryScope.Value = context == null ? null : new QueryScope(context); + var contextScope = Use(context); + return new DisposeAction(() => + { + contextScope.Dispose(); + _queryScope.Value = previousScope; + }); + } + + internal void AuthorizeQueryCompilation(IAbpEfCoreDbFunctionContext context, Expression query) + { + var scope = _queryScope.Value; + if (scope != null && scope.Context == context) + { + scope.AuthorizedQuery = query; + } + } + + internal IDisposable BeginQueryCompilation(IAbpEfCoreDbFunctionContext? context, Expression query) + { + var scope = _queryScope.Value; + if (scope == null || scope.Context != context || !ReferenceEquals(scope.AuthorizedQuery, query)) + { + return Use(null); + } + + scope.AuthorizedQuery = null; + return Use(scope.Context); + } + + private sealed class QueryScope + { + public IAbpEfCoreDbFunctionContext Context { get; } + + public Expression? AuthorizedQuery { get; set; } + + public QueryScope(IAbpEfCoreDbFunctionContext context) + { + Context = context; + } + } } diff --git a/framework/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/EntityFrameworkCore/GlobalFilters/AbpEfCoreDatabase.cs b/framework/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/EntityFrameworkCore/GlobalFilters/AbpEfCoreDatabase.cs new file mode 100644 index 0000000000..92fe77003b --- /dev/null +++ b/framework/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/EntityFrameworkCore/GlobalFilters/AbpEfCoreDatabase.cs @@ -0,0 +1,56 @@ +using System; +using System.Collections.Generic; +using System.Linq.Expressions; +using System.Threading; +using System.Threading.Tasks; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Query; +using Microsoft.EntityFrameworkCore.Storage; +using Microsoft.EntityFrameworkCore.Update; + +namespace Volo.Abp.EntityFrameworkCore.GlobalFilters; + +public class AbpEfCoreDatabase : IDatabase +{ + protected IDatabase InnerDatabase { get; } + protected AbpEfCoreCurrentDbContext AbpEfCoreCurrentDbContext { get; } + protected ICurrentDbContext CurrentDbContext { get; } + + public AbpEfCoreDatabase( + IDatabase innerDatabase, + AbpEfCoreCurrentDbContext abpEfCoreCurrentDbContext, + ICurrentDbContext currentDbContext) + { + InnerDatabase = innerDatabase; + AbpEfCoreCurrentDbContext = abpEfCoreCurrentDbContext; + CurrentDbContext = currentDbContext; + } + + public virtual int SaveChanges(IList entries) + { + return InnerDatabase.SaveChanges(entries); + } + + public virtual Task SaveChangesAsync(IList entries, CancellationToken cancellationToken = default) + { + return InnerDatabase.SaveChangesAsync(entries, cancellationToken); + } + + public virtual Func CompileQuery(Expression query, bool async) + { + using (AbpEfCoreCurrentDbContext.BeginQueryCompilation(CurrentDbContext.Context as IAbpEfCoreDbFunctionContext, query)) + { + return InnerDatabase.CompileQuery(query, async); + } + } + +#pragma warning disable EF9100 + public virtual Expression> CompileQueryExpression(Expression query, bool async) + { + using (AbpEfCoreCurrentDbContext.BeginQueryCompilation(CurrentDbContext.Context as IAbpEfCoreDbFunctionContext, query)) + { + return InnerDatabase.CompileQueryExpression(query, async); + } + } +#pragma warning restore EF9100 +} diff --git a/framework/test/Volo.Abp.EntityFrameworkCore.Tests/Volo/Abp/EntityFrameworkCore/DataFiltering/CompiledQueryCache/AbpDbContextOptionsExtension_Tests.cs b/framework/test/Volo.Abp.EntityFrameworkCore.Tests/Volo/Abp/EntityFrameworkCore/DataFiltering/CompiledQueryCache/AbpDbContextOptionsExtension_Tests.cs new file mode 100644 index 0000000000..d2759e1fd0 --- /dev/null +++ b/framework/test/Volo.Abp.EntityFrameworkCore.Tests/Volo/Abp/EntityFrameworkCore/DataFiltering/CompiledQueryCache/AbpDbContextOptionsExtension_Tests.cs @@ -0,0 +1,77 @@ +using System; +using System.Linq; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Query; +using Microsoft.EntityFrameworkCore.Storage; +using Microsoft.Extensions.DependencyInjection; +using Shouldly; +using Volo.Abp.EntityFrameworkCore.GlobalFilters; +using Xunit; + +namespace Volo.Abp.EntityFrameworkCore.DataFiltering.CompiledQueryCache; + +public class AbpDbContextOptionsExtension_Tests +{ + [Fact] + public void Should_Create_And_Delete_InMemory_Database() + { + var options = new DbContextOptionsBuilder() + .UseInMemoryDatabase(Guid.NewGuid().ToString()) + .AddAbpDbContextOptionsExtension() + .Options; + + using var context = new DbContext(options); + context.GetService().ShouldNotBeOfType(); + context.Database.EnsureCreated().ShouldBeTrue(); + context.Database.EnsureDeleted().ShouldBeTrue(); + } + + [Fact] + public void Should_Apply_Services_Once() + { + var services = new ServiceCollection(); + services.AddEntityFrameworkSqlite(); + + new AbpDbContextOptionsExtension().ApplyServices(services); + new AbpDbContextOptionsExtension().ApplyServices(services); + + services.Count(x => x.ServiceType == typeof(IDatabase) && x.IsKeyedService).ShouldBe(1); + services.Count(x => x.ServiceType == typeof(AbpEfCoreCurrentDbContext)).ShouldBe(1); + } + + [Fact] + public void Should_Apply_Services_Again_After_The_Database_Provider() + { + var services = new ServiceCollection(); + var extension = new AbpDbContextOptionsExtension(); + + extension.ApplyServices(services); + services.AddEntityFrameworkSqlite(); + extension.ApplyServices(services); + + services.Count(x => x.ServiceType == typeof(IDatabase) && x.IsKeyedService).ShouldBe(1); + services.Count(x => x.ServiceType == typeof(ICompiledQueryCacheKeyGenerator) && x.IsKeyedService).ShouldBe(1); + services.Count(x => x.ServiceType == typeof(AbpEfCoreCurrentDbContext)).ShouldBe(1); + } + + [Fact] + public void Should_Decorate_Database_Registered_After_Applying_Services() + { + var services = new ServiceCollection(); + var extension = new AbpDbContextOptionsExtension(); + services.AddEntityFrameworkSqlite(); + extension.ApplyServices(services); + services.AddScoped(provider => ActivatorUtilities.CreateInstance(provider)); + extension.ApplyServices(services); + + using var serviceProvider = services.BuildServiceProvider(); + var options = new DbContextOptionsBuilder() + .UseSqlite("Data Source=:memory:") + .UseInternalServiceProvider(serviceProvider) + .Options; + + using var context = new DbContext(options); + context.GetService().ShouldBeOfType(); + } +} diff --git a/framework/test/Volo.Abp.EntityFrameworkCore.Tests/Volo/Abp/EntityFrameworkCore/DataFiltering/CompiledQueryCache/CompiledQueryCacheTestBase.cs b/framework/test/Volo.Abp.EntityFrameworkCore.Tests/Volo/Abp/EntityFrameworkCore/DataFiltering/CompiledQueryCache/CompiledQueryCacheTestBase.cs index c8de61e946..d3ca9a733e 100644 --- a/framework/test/Volo.Abp.EntityFrameworkCore.Tests/Volo/Abp/EntityFrameworkCore/DataFiltering/CompiledQueryCache/CompiledQueryCacheTestBase.cs +++ b/framework/test/Volo.Abp.EntityFrameworkCore.Tests/Volo/Abp/EntityFrameworkCore/DataFiltering/CompiledQueryCache/CompiledQueryCacheTestBase.cs @@ -155,6 +155,27 @@ public abstract class CompiledQueryCacheTestBase : TestAppTestBa await uow.CompleteAsync(); } + [Fact] + public async Task Should_Apply_Filters_To_Other_Query_Entry_Points() + { + var names = await RunInTenantAsync(CompiledQueryCacheTestModuleBase.TenantA, async db => + { + (await db.Rows.IgnoreQueryFilters().CountAsync()).ShouldBe(CompiledQueryCacheTestModuleBase.Rows.Length); + db.Rows.Select(row => row.Name).ToQueryString().ShouldContain("IsDeleted"); + + (await db.Rows.Where(row => row.Name == "B-live").ExecuteUpdateAsync(s => s.SetProperty(row => row.Name, "B-updated"))).ShouldBe(0); + (await db.Rows.Where(row => row.Name == "A-live").ExecuteUpdateAsync(s => s.SetProperty(row => row.Name, "A-updated"))).ShouldBe(1); + + var row = await db.Rows.SingleAsync(); + row.Name = "A-saved"; + await db.SaveChangesAsync(); + + return await db.Rows.AsNoTracking().Select(x => x.Name).ToListAsync(); + }); + + names.ShouldBe(new[] { "A-saved" }); + } + protected async Task> RunInTenantAsync( Guid? tenantId, Func>> query) diff --git a/framework/test/Volo.Abp.EntityFrameworkCore.Tests/Volo/Abp/EntityFrameworkCore/DataFiltering/CompiledQueryCache/CompiledQueryCacheTestDatabase.cs b/framework/test/Volo.Abp.EntityFrameworkCore.Tests/Volo/Abp/EntityFrameworkCore/DataFiltering/CompiledQueryCache/CompiledQueryCacheTestDatabase.cs new file mode 100644 index 0000000000..580e33bd71 --- /dev/null +++ b/framework/test/Volo.Abp.EntityFrameworkCore.Tests/Volo/Abp/EntityFrameworkCore/DataFiltering/CompiledQueryCache/CompiledQueryCacheTestDatabase.cs @@ -0,0 +1,46 @@ +using System; +using System.Collections.Generic; +using System.Threading; +using System.Threading.Tasks; +using Microsoft.EntityFrameworkCore.Storage; +using Microsoft.EntityFrameworkCore.Update; + +namespace Volo.Abp.EntityFrameworkCore.DataFiltering.CompiledQueryCache; + +public class CompiledQueryCacheTestDatabase : RelationalDatabase, IDisposable +{ + private readonly CompiledQueryCacheTestDatabaseRecorder _recorder; + + public CompiledQueryCacheTestDatabase( + DatabaseDependencies dependencies, + RelationalDatabaseDependencies relationalDependencies, + CompiledQueryCacheTestDatabaseRecorder recorder) + : base(dependencies, relationalDependencies) + { + _recorder = recorder; + } + + public override int SaveChanges(IList entries) + { + Interlocked.Increment(ref _recorder.SaveChangesCount); + return base.SaveChanges(entries); + } + + public override Task SaveChangesAsync(IList entries, CancellationToken cancellationToken = default) + { + Interlocked.Increment(ref _recorder.SaveChangesCount); + return base.SaveChangesAsync(entries, cancellationToken); + } + + public void Dispose() + { + Interlocked.Increment(ref _recorder.DisposeCount); + } +} + +public class CompiledQueryCacheTestDatabaseRecorder +{ + public int SaveChangesCount; + + public int DisposeCount; +} diff --git a/framework/test/Volo.Abp.EntityFrameworkCore.Tests/Volo/Abp/EntityFrameworkCore/DataFiltering/CompiledQueryCache/CompiledQueryCacheTestHooks.cs b/framework/test/Volo.Abp.EntityFrameworkCore.Tests/Volo/Abp/EntityFrameworkCore/DataFiltering/CompiledQueryCache/CompiledQueryCacheTestHooks.cs new file mode 100644 index 0000000000..89d94ad631 --- /dev/null +++ b/framework/test/Volo.Abp.EntityFrameworkCore.Tests/Volo/Abp/EntityFrameworkCore/DataFiltering/CompiledQueryCache/CompiledQueryCacheTestHooks.cs @@ -0,0 +1,49 @@ +using System; +using System.Linq.Expressions; +using Microsoft.EntityFrameworkCore.Diagnostics; +using Volo.Abp.EntityFrameworkCore.GlobalFilters; + +namespace Volo.Abp.EntityFrameworkCore.DataFiltering.CompiledQueryCache; + +public class CompiledQueryCacheTestHooks +{ + public Action OnQueryCompilationStarting { get; set; } + + public Action OnGetCompiledQueryCacheKey { get; set; } +} + +public class CompiledQueryCacheTestInterceptor : IQueryExpressionInterceptor +{ + private readonly CompiledQueryCacheTestHooks _hooks; + + public CompiledQueryCacheTestInterceptor(CompiledQueryCacheTestHooks hooks) + { + _hooks = hooks; + } + + public Expression QueryCompilationStarting(Expression queryExpression, QueryExpressionEventData eventData) + { + if (eventData.Context is CompiledQueryCacheTestDbContext dbContext) + { + _hooks.OnQueryCompilationStarting?.Invoke(dbContext); + } + + return queryExpression; + } +} + +public class CompiledQueryCacheTestKeyProvider : IAbpEfCoreCompiledQueryCacheKeyProvider +{ + private readonly CompiledQueryCacheTestHooks _hooks; + + public CompiledQueryCacheTestKeyProvider(CompiledQueryCacheTestHooks hooks) + { + _hooks = hooks; + } + + public string GetCompiledQueryCacheKey() + { + _hooks.OnGetCompiledQueryCacheKey?.Invoke(); + return null; + } +} diff --git a/framework/test/Volo.Abp.EntityFrameworkCore.Tests/Volo/Abp/EntityFrameworkCore/DataFiltering/CompiledQueryCache/CompiledQueryCacheTestModuleBase.cs b/framework/test/Volo.Abp.EntityFrameworkCore.Tests/Volo/Abp/EntityFrameworkCore/DataFiltering/CompiledQueryCache/CompiledQueryCacheTestModuleBase.cs index 2724d2e065..7f8552d7cd 100644 --- a/framework/test/Volo.Abp.EntityFrameworkCore.Tests/Volo/Abp/EntityFrameworkCore/DataFiltering/CompiledQueryCache/CompiledQueryCacheTestModuleBase.cs +++ b/framework/test/Volo.Abp.EntityFrameworkCore.Tests/Volo/Abp/EntityFrameworkCore/DataFiltering/CompiledQueryCache/CompiledQueryCacheTestModuleBase.cs @@ -1,6 +1,7 @@ using System; using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore.Diagnostics; +using Microsoft.EntityFrameworkCore.Storage; using Microsoft.Extensions.DependencyInjection; using Volo.Abp.Data; using Volo.Abp.EntityFrameworkCore.GlobalFilters; @@ -29,20 +30,52 @@ public abstract class CompiledQueryCacheTestModuleBase : AbpModule protected abstract bool UseDbFunction { get; } + protected virtual bool AddAbpServicesBeforeDatabaseProvider => false; + + protected virtual bool AddCustomDatabase => false; + + // When false, EF Core builds its own service provider, so it applies the ReplaceService calls of the options. + protected virtual bool UseExternalEfServiceProvider => true; + + protected virtual void ConfigureEfOptions(DbContextOptionsBuilder options) + { + } + public override void ConfigureServices(ServiceConfigurationContext context) { var counter = new QueryCompilationCounter(); + var hooks = new CompiledQueryCacheTestHooks(); + var databaseRecorder = new CompiledQueryCacheTestDatabaseRecorder(); - // Avoid growing EF's global service-provider cache across test applications. - var efServices = new ServiceCollection(); - efServices.AddEntityFrameworkSqlite(); - if (UseDbFunction) + if (UseExternalEfServiceProvider) { - new AbpDbContextOptionsExtension().ApplyServices(efServices); + // Avoid growing EF's global service-provider cache across test applications. + var efServices = new ServiceCollection(); + if (UseDbFunction && AddAbpServicesBeforeDatabaseProvider) + { + new AbpDbContextOptionsExtension().ApplyServices(efServices); + } + + efServices.AddEntityFrameworkSqlite(); + if (AddCustomDatabase) + { + efServices.AddSingleton(databaseRecorder); + efServices.AddScoped(provider => ActivatorUtilities.CreateInstance(provider)); + } + + efServices.AddSingleton(new CompiledQueryCacheTestInterceptor(hooks)); + if (UseDbFunction && !AddAbpServicesBeforeDatabaseProvider) + { + new AbpDbContextOptionsExtension().ApplyServices(efServices); + } + + _efServiceProvider = efServices.BuildServiceProvider(); } - _efServiceProvider = efServices.BuildServiceProvider(); context.Services.AddSingleton(counter); + context.Services.AddSingleton(hooks); + context.Services.AddSingleton(databaseRecorder); + context.Services.AddTransient(_ => new CompiledQueryCacheTestKeyProvider(hooks)); context.Services.AddAbpDbContext(); Configure(options => options.UseDbFunction = UseDbFunction); Configure(options => options.ConnectionStrings.Default = _database.ConnectionString); @@ -50,7 +83,17 @@ public abstract class CompiledQueryCacheTestModuleBase : AbpModule { configuration.UseSqlite(); configuration.DbContextOptions.UseLoggerFactory(null); - configuration.DbContextOptions.UseInternalServiceProvider(_efServiceProvider); + if (UseExternalEfServiceProvider) + { + configuration.DbContextOptions.UseInternalServiceProvider(_efServiceProvider); + } + else + { + configuration.DbContextOptions.EnableServiceProviderCaching(false); + configuration.DbContextOptions.AddInterceptors(new CompiledQueryCacheTestInterceptor(hooks)); + } + + ConfigureEfOptions(configuration.DbContextOptions); configuration.DbContextOptions.LogTo( _ => counter.Increment(), new[] { CoreEventId.QueryCompilationStarting }); @@ -74,6 +117,6 @@ public abstract class CompiledQueryCacheTestModuleBase : AbpModule public override void OnApplicationShutdown(ApplicationShutdownContext context) { _database.Dispose(); - _efServiceProvider.Dispose(); + _efServiceProvider?.Dispose(); } } diff --git a/framework/test/Volo.Abp.EntityFrameworkCore.Tests/Volo/Abp/EntityFrameworkCore/DataFiltering/CompiledQueryCache/CompiledQueryCacheTestModuleWithAbpServicesFirst.cs b/framework/test/Volo.Abp.EntityFrameworkCore.Tests/Volo/Abp/EntityFrameworkCore/DataFiltering/CompiledQueryCache/CompiledQueryCacheTestModuleWithAbpServicesFirst.cs new file mode 100644 index 0000000000..9fc58b9070 --- /dev/null +++ b/framework/test/Volo.Abp.EntityFrameworkCore.Tests/Volo/Abp/EntityFrameworkCore/DataFiltering/CompiledQueryCache/CompiledQueryCacheTestModuleWithAbpServicesFirst.cs @@ -0,0 +1,13 @@ +using Volo.Abp.Autofac; +using Volo.Abp.EntityFrameworkCore.Sqlite; +using Volo.Abp.Modularity; + +namespace Volo.Abp.EntityFrameworkCore.DataFiltering.CompiledQueryCache; + +[DependsOn(typeof(AbpEntityFrameworkCoreSqliteModule), typeof(AbpAutofacModule))] +public class CompiledQueryCacheTestModuleWithAbpServicesFirst : CompiledQueryCacheTestModuleBase +{ + protected override bool UseDbFunction => true; + + protected override bool AddAbpServicesBeforeDatabaseProvider => true; +} diff --git a/framework/test/Volo.Abp.EntityFrameworkCore.Tests/Volo/Abp/EntityFrameworkCore/DataFiltering/CompiledQueryCache/CompiledQueryCacheTestModuleWithDatabaseFactory.cs b/framework/test/Volo.Abp.EntityFrameworkCore.Tests/Volo/Abp/EntityFrameworkCore/DataFiltering/CompiledQueryCache/CompiledQueryCacheTestModuleWithDatabaseFactory.cs new file mode 100644 index 0000000000..b7aeafc846 --- /dev/null +++ b/framework/test/Volo.Abp.EntityFrameworkCore.Tests/Volo/Abp/EntityFrameworkCore/DataFiltering/CompiledQueryCache/CompiledQueryCacheTestModuleWithDatabaseFactory.cs @@ -0,0 +1,13 @@ +using Volo.Abp.Autofac; +using Volo.Abp.EntityFrameworkCore.Sqlite; +using Volo.Abp.Modularity; + +namespace Volo.Abp.EntityFrameworkCore.DataFiltering.CompiledQueryCache; + +[DependsOn(typeof(AbpEntityFrameworkCoreSqliteModule), typeof(AbpAutofacModule))] +public class CompiledQueryCacheTestModuleWithDatabaseFactory : CompiledQueryCacheTestModuleBase +{ + protected override bool UseDbFunction => true; + + protected override bool AddCustomDatabase => true; +} diff --git a/framework/test/Volo.Abp.EntityFrameworkCore.Tests/Volo/Abp/EntityFrameworkCore/DataFiltering/CompiledQueryCache/CompiledQueryCacheTestModuleWithQueryCompiler.cs b/framework/test/Volo.Abp.EntityFrameworkCore.Tests/Volo/Abp/EntityFrameworkCore/DataFiltering/CompiledQueryCache/CompiledQueryCacheTestModuleWithQueryCompiler.cs new file mode 100644 index 0000000000..abd4b085e4 --- /dev/null +++ b/framework/test/Volo.Abp.EntityFrameworkCore.Tests/Volo/Abp/EntityFrameworkCore/DataFiltering/CompiledQueryCache/CompiledQueryCacheTestModuleWithQueryCompiler.cs @@ -0,0 +1,43 @@ +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Query.Internal; +using Microsoft.Extensions.DependencyInjection; +using Volo.Abp.Autofac; +using Volo.Abp.EntityFrameworkCore.Sqlite; +using Volo.Abp.Modularity; + +namespace Volo.Abp.EntityFrameworkCore.DataFiltering.CompiledQueryCache; + +[DependsOn(typeof(AbpEntityFrameworkCoreSqliteModule), typeof(AbpAutofacModule))] +public class CompiledQueryCacheTestModuleWithQueryCompiler : CompiledQueryCacheTestModuleBase +{ + private readonly CompiledQueryCacheTestQueryCompilerOptions _queryCompilerOptions = new(); + + protected override bool UseDbFunction => true; + + protected override bool UseExternalEfServiceProvider => false; + + public override void ConfigureServices(ServiceConfigurationContext context) + { + context.Services.AddSingleton(_queryCompilerOptions); + base.ConfigureServices(context); + } + + protected override void ConfigureEfOptions(DbContextOptionsBuilder options) + { +#pragma warning disable EF1001 + if (_queryCompilerOptions.ReplaceImplementationType) + { + options.ReplaceService(); + } + else + { + options.ReplaceService(); + } +#pragma warning restore EF1001 + } +} + +public class CompiledQueryCacheTestQueryCompilerOptions +{ + public bool ReplaceImplementationType { get; set; } +} diff --git a/framework/test/Volo.Abp.EntityFrameworkCore.Tests/Volo/Abp/EntityFrameworkCore/DataFiltering/CompiledQueryCache/CompiledQueryCacheTestQueryCompiler.cs b/framework/test/Volo.Abp.EntityFrameworkCore.Tests/Volo/Abp/EntityFrameworkCore/DataFiltering/CompiledQueryCache/CompiledQueryCacheTestQueryCompiler.cs new file mode 100644 index 0000000000..83a3067ebf --- /dev/null +++ b/framework/test/Volo.Abp.EntityFrameworkCore.Tests/Volo/Abp/EntityFrameworkCore/DataFiltering/CompiledQueryCache/CompiledQueryCacheTestQueryCompiler.cs @@ -0,0 +1,56 @@ +using System; +using System.Linq.Expressions; +using System.Threading; +using Microsoft.EntityFrameworkCore.Query; +using Microsoft.EntityFrameworkCore.Query.Internal; +using Microsoft.Extensions.DependencyInjection; + +namespace Volo.Abp.EntityFrameworkCore.DataFiltering.CompiledQueryCache; + +#pragma warning disable EF1001 +// A custom query compiler that an application registers with ReplaceService. +public class CompiledQueryCacheTestQueryCompiler : IQueryCompiler +{ + private readonly QueryCompiler _inner; + + public int ExecuteCount { get; private set; } + + public int CreateCompiledQueryCount { get; private set; } + + public CompiledQueryCacheTestQueryCompiler(IServiceProvider serviceProvider) + { + _inner = ActivatorUtilities.CreateInstance(serviceProvider); + } + + public TResult Execute(Expression query) + { + ExecuteCount++; + return _inner.Execute(query); + } + + public TResult ExecuteAsync(Expression query, CancellationToken cancellationToken) + { + ExecuteCount++; + return _inner.ExecuteAsync(query, cancellationToken); + } + + public Func CreateCompiledQuery(Expression query) + { + CreateCompiledQueryCount++; + return _inner.CreateCompiledQuery(query); + } + + public Func CreateCompiledAsyncQuery(Expression query) + { + CreateCompiledQueryCount++; + return _inner.CreateCompiledAsyncQuery(query); + } + +#pragma warning disable EF9100 + public Expression> PrecompileQuery(Expression query, bool async) + { + return _inner.PrecompileQuery(query, async); + } +#pragma warning restore EF9100 +} +#pragma warning restore EF1001 diff --git a/framework/test/Volo.Abp.EntityFrameworkCore.Tests/Volo/Abp/EntityFrameworkCore/DataFiltering/CompiledQueryCache/CompiledQueryCache_AbpServicesFirst_Tests.cs b/framework/test/Volo.Abp.EntityFrameworkCore.Tests/Volo/Abp/EntityFrameworkCore/DataFiltering/CompiledQueryCache/CompiledQueryCache_AbpServicesFirst_Tests.cs new file mode 100644 index 0000000000..59bba7fcd6 --- /dev/null +++ b/framework/test/Volo.Abp.EntityFrameworkCore.Tests/Volo/Abp/EntityFrameworkCore/DataFiltering/CompiledQueryCache/CompiledQueryCache_AbpServicesFirst_Tests.cs @@ -0,0 +1,25 @@ +using System.Linq; +using System.Threading.Tasks; +using Microsoft.EntityFrameworkCore; +using Shouldly; +using Volo.Abp.MultiTenancy; +using Volo.Abp.TestApp.Testing; +using Volo.Abp.Uow; +using Xunit; + +namespace Volo.Abp.EntityFrameworkCore.DataFiltering.CompiledQueryCache; + +public class CompiledQueryCache_AbpServicesFirst_Tests : TestAppTestBase +{ + [Fact] + public async Task Should_Not_Run_Query_Without_Filters() + { + using var uow = GetRequiredService().Begin(); + using var tenant = GetRequiredService().Change(CompiledQueryCacheTestModuleBase.TenantA); + var context = GetRequiredService(); + context.UseDbFunction().ShouldBeTrue(); + + var exception = await Should.ThrowAsync(() => context.Rows.Select(row => row.Name).ToListAsync()); + exception.Message.ShouldContain("AbpDbContextOptionsExtension"); + } +} diff --git a/framework/test/Volo.Abp.EntityFrameworkCore.Tests/Volo/Abp/EntityFrameworkCore/DataFiltering/CompiledQueryCache/CompiledQueryCache_DatabaseFactory_Tests.cs b/framework/test/Volo.Abp.EntityFrameworkCore.Tests/Volo/Abp/EntityFrameworkCore/DataFiltering/CompiledQueryCache/CompiledQueryCache_DatabaseFactory_Tests.cs new file mode 100644 index 0000000000..c9b0a7a4bd --- /dev/null +++ b/framework/test/Volo.Abp.EntityFrameworkCore.Tests/Volo/Abp/EntityFrameworkCore/DataFiltering/CompiledQueryCache/CompiledQueryCache_DatabaseFactory_Tests.cs @@ -0,0 +1,49 @@ +using System; +using System.Linq; +using System.Threading.Tasks; +using Microsoft.EntityFrameworkCore; +using Microsoft.Extensions.DependencyInjection; +using Shouldly; +using Volo.Abp.MultiTenancy; +using Volo.Abp.Uow; +using Xunit; + +namespace Volo.Abp.EntityFrameworkCore.DataFiltering.CompiledQueryCache; + +public class CompiledQueryCache_DatabaseFactory_Tests : CompiledQueryCacheTestBase +{ + protected override bool UseDbFunction => true; + + [Fact] + public async Task Should_Not_Run_Compiled_Query_First_Run_Inside_Another_Query_Without_Filters() + { + var countRows = EF.CompileQuery((CompiledQueryCacheTestDbContext db) => db.Rows.Count()); + + var exception = await Should.ThrowAsync(() => + RunInTenantAsync(CompiledQueryCacheTestModuleBase.TenantA, db => + Task.FromResult(db.Rows.Where(row => row.Name.Length > countRows(db)).Select(row => row.Name).ToList()))); + exception.InnerException.ShouldBeOfType(); + } + + [Fact] + public async Task Should_Keep_Using_And_Disposing_The_Custom_Database() + { + var recorder = GetRequiredService(); + var disposeCount = recorder.DisposeCount; + + using (var scope = ServiceProvider.CreateScope()) + { + using var uow = scope.ServiceProvider.GetRequiredService().Begin(); + using var tenant = scope.ServiceProvider.GetRequiredService().Change(CompiledQueryCacheTestModuleBase.TenantA); + var db = scope.ServiceProvider.GetRequiredService(); + + var row = await db.Rows.SingleAsync(); + row.Name = "A-saved"; + await db.SaveChangesAsync(); + await uow.CompleteAsync(); + } + + recorder.SaveChangesCount.ShouldBe(1); + recorder.DisposeCount.ShouldBeGreaterThan(disposeCount); + } +} diff --git a/framework/test/Volo.Abp.EntityFrameworkCore.Tests/Volo/Abp/EntityFrameworkCore/DataFiltering/CompiledQueryCache/CompiledQueryCache_QueryCompiler_Tests.cs b/framework/test/Volo.Abp.EntityFrameworkCore.Tests/Volo/Abp/EntityFrameworkCore/DataFiltering/CompiledQueryCache/CompiledQueryCache_QueryCompiler_Tests.cs new file mode 100644 index 0000000000..57f0a341ef --- /dev/null +++ b/framework/test/Volo.Abp.EntityFrameworkCore.Tests/Volo/Abp/EntityFrameworkCore/DataFiltering/CompiledQueryCache/CompiledQueryCache_QueryCompiler_Tests.cs @@ -0,0 +1,75 @@ +using System; +using System.Linq; +using System.Threading.Tasks; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Query.Internal; +using Shouldly; +using Volo.Abp.Data; +using Volo.Abp.MultiTenancy; +using Volo.Abp.TestApp.Testing; +using Xunit; + +namespace Volo.Abp.EntityFrameworkCore.DataFiltering.CompiledQueryCache; + +#pragma warning disable EF1001 +public class CompiledQueryCache_QueryCompiler_Tests : TestAppTestBase +{ + [Theory] + [InlineData(false)] + [InlineData(true)] + public async Task Should_Use_The_Replaced_Query_Compiler(bool replaceImplementationType) + { + GetRequiredService().ReplaceImplementationType = replaceImplementationType; + + await WithUnitOfWorkAsync(async () => + { + var currentTenant = GetRequiredService(); + using (currentTenant.Change(CompiledQueryCacheTestModuleBase.TenantA)) + { + var db = GetRequiredService(); + db.UseDbFunction().ShouldBeTrue(); + var queryCompiler = db.GetService().ShouldBeOfType(); + + (await db.Rows.Select(row => row.Name).ToListAsync()).ShouldBe(new[] { "A-live" }); + using (currentTenant.Change(CompiledQueryCacheTestModuleBase.TenantB)) + { + db.Rows.Select(row => row.Name).ToList().ShouldBe(new[] { "B-live" }); + } + + queryCompiler.ExecuteCount.ShouldBe(2); + } + }); + } + + [Theory] + [InlineData(false)] + [InlineData(true)] + public async Task Should_Not_Run_Compiled_Query_First_Run_Inside_Another_Query_Without_Filters(bool replaceImplementationType) + { + GetRequiredService().ReplaceImplementationType = replaceImplementationType; + var countRows = EF.CompileQuery((CompiledQueryCacheTestDbContext db) => db.Rows.Count()); + + await WithUnitOfWorkAsync(async () => + { + using (GetRequiredService().Change(CompiledQueryCacheTestModuleBase.TenantA)) + { + var db = GetRequiredService(); + var queryCompiler = db.GetService().ShouldBeOfType(); + var dataFilter = GetRequiredService(); + + using (dataFilter.Disable()) + using (dataFilter.Disable()) + { + var exception = Should.Throw(() => + db.Rows.Where(row => row.Name.Length > countRows(db)).Select(row => row.Name).ToList()); + exception.InnerException.ShouldBeOfType(); + } + + queryCompiler.CreateCompiledQueryCount.ShouldBe(1); + (await db.Rows.Select(row => row.Name).ToListAsync()).ShouldBe(new[] { "A-live" }); + } + }); + } +} +#pragma warning restore EF1001 diff --git a/framework/test/Volo.Abp.EntityFrameworkCore.Tests/Volo/Abp/EntityFrameworkCore/DataFiltering/CompiledQueryCache/CompiledQueryCache_Tests.cs b/framework/test/Volo.Abp.EntityFrameworkCore.Tests/Volo/Abp/EntityFrameworkCore/DataFiltering/CompiledQueryCache/CompiledQueryCache_Tests.cs index 2d9fe405a2..146ef79eb2 100644 --- a/framework/test/Volo.Abp.EntityFrameworkCore.Tests/Volo/Abp/EntityFrameworkCore/DataFiltering/CompiledQueryCache/CompiledQueryCache_Tests.cs +++ b/framework/test/Volo.Abp.EntityFrameworkCore.Tests/Volo/Abp/EntityFrameworkCore/DataFiltering/CompiledQueryCache/CompiledQueryCache_Tests.cs @@ -1,8 +1,16 @@ using System; +using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Query; +using Microsoft.EntityFrameworkCore.Storage; +using Microsoft.Extensions.DependencyInjection; using Shouldly; +using Volo.Abp.Data; +using Volo.Abp.EntityFrameworkCore.GlobalFilters; +using Volo.Abp.MultiTenancy; using Xunit; namespace Volo.Abp.EntityFrameworkCore.DataFiltering.CompiledQueryCache; @@ -11,8 +19,6 @@ public class CompiledQueryCache_Tests : CompiledQueryCacheTestBase true; - // EF.CompileQuery translates the query outside of AbpEntityQueryProvider, so the filter state is not available. - // Each filter is tested on its own entity, so one translation cannot hide a missing check in the other. [Fact] public async Task Should_Not_Run_Compiled_Query_Without_SoftDelete_Filter() { @@ -31,7 +37,6 @@ public class CompiledQueryCache_Tests : CompiledQueryCacheTestBase Task.FromResult(query(db).ToList()))); } - // The compiled query is first run while the outer query is executed, so the current DbContext of the outer query is set. [Fact] public async Task Should_Not_Run_Compiled_Query_First_Run_Inside_Another_Query_Without_Filters() { @@ -64,6 +69,209 @@ public class CompiledQueryCache_Tests : CompiledQueryCacheTestBase new[] { (await countRowsAsync(db)).ToString() }.ToList())); } + [Theory] + [InlineData(false)] + [InlineData(true)] + public async Task Should_Not_Run_Compiled_Query_First_Run_Inside_Another_Query_With_Disabled_Filters(bool asyncCompiledQuery) + { + var countRows = EF.CompileQuery((CompiledQueryCacheTestDbContext db) => db.Rows.Count()); + var countRowsAsync = EF.CompileAsyncQuery((CompiledQueryCacheTestDbContext db) => db.Rows.Count()); + var dataFilter = GetRequiredService(); + + using (dataFilter.Disable()) + using (dataFilter.Disable()) + { + var exception = await Should.ThrowAsync(() => + RunInTenantAsync(CompiledQueryCacheTestModuleBase.TenantA, db => + Task.FromResult(asyncCompiledQuery + ? db.Rows.Where(row => row.Name.Length > countRowsAsync(db).GetAwaiter().GetResult()).Select(row => row.Name).ToList() + : db.Rows.Where(row => row.Name.Length > countRows(db)).Select(row => row.Name).ToList()))); + exception.InnerException.ShouldBeOfType(); + } + + await Should.ThrowAsync(() => + RunInTenantAsync(CompiledQueryCacheTestModuleBase.TenantA, async db => + new[] { (asyncCompiledQuery ? await countRowsAsync(db) : countRows(db)).ToString() }.ToList())); + } + + [Fact] + public async Task Should_Not_Run_Compiled_Query_First_Run_While_Another_Query_Is_Compiled_Without_Filters() + { + var countRows = EF.CompileQuery((CompiledQueryCacheTestDbContext db) => db.Rows.Count()); + var countRowsAsync = EF.CompileAsyncQuery((CompiledQueryCacheTestDbContext db) => db.Rows.Count()); + var hooks = GetRequiredService(); + + hooks.OnQueryCompilationStarting = db => + { + hooks.OnQueryCompilationStarting = null; + countRows(db); + }; + await Should.ThrowAsync(() => + RunInTenantAsync(CompiledQueryCacheTestModuleBase.TenantA, db => + Task.FromResult(db.Rows.TagWith(Guid.NewGuid().ToString()).Select(row => row.Name).ToList()))); + + hooks.OnQueryCompilationStarting = db => + { + hooks.OnQueryCompilationStarting = null; + countRowsAsync(db).GetAwaiter().GetResult(); + }; + await Should.ThrowAsync(() => + RunInTenantAsync(CompiledQueryCacheTestModuleBase.TenantA, db => + db.Rows.TagWith(Guid.NewGuid().ToString()).Select(row => row.Name).ToListAsync())); + + await Should.ThrowAsync(() => + RunInTenantAsync(CompiledQueryCacheTestModuleBase.TenantA, db => Task.FromResult(new[] { countRows(db).ToString() }.ToList()))); + } + + [Fact] + public async Task Should_Not_Run_Compiled_Query_First_Run_While_Cache_Key_Is_Generated_Without_Filters() + { + var countRows = EF.CompileQuery((CompiledQueryCacheTestDbContext db) => db.Rows.Count()); + var hooks = GetRequiredService(); + + await Should.ThrowAsync(() => + RunInTenantAsync(CompiledQueryCacheTestModuleBase.TenantA, db => + { + hooks.OnGetCompiledQueryCacheKey = () => + { + hooks.OnGetCompiledQueryCacheKey = null; + countRows(db); + }; + + return Task.FromResult(db.Rows.TagWith(Guid.NewGuid().ToString()).Select(row => row.Name).ToList()); + })); + + await Should.ThrowAsync(() => + RunInTenantAsync(CompiledQueryCacheTestModuleBase.TenantA, db => Task.FromResult(new[] { countRows(db).ToString() }.ToList()))); + } + + [Theory] + [InlineData(false)] + [InlineData(true)] + public async Task Should_Not_Precompile_Query_While_Cache_Key_Is_Generated_Without_Filters(bool async) + { + var hooks = GetRequiredService(); + + await Should.ThrowAsync(() => + RunInTenantAsync(CompiledQueryCacheTestModuleBase.TenantA, db => + { + hooks.OnGetCompiledQueryCacheKey = () => + { + hooks.OnGetCompiledQueryCacheKey = null; +#pragma warning disable EF1001, EF9100 + var queryCompiler = db.GetService(); + var query = db.Rows.Select(row => row.Name).Expression; + if (async) + { + queryCompiler.PrecompileQuery>(query, true); + } + else + { + queryCompiler.PrecompileQuery>(query, false); + } +#pragma warning restore EF1001, EF9100 + }; + + return Task.FromResult(db.Rows.TagWith(Guid.NewGuid().ToString()).Select(row => row.Name).ToList()); + })); + } + + [Fact] + public async Task Should_Provide_Current_DbContext_While_Cache_Key_Is_Generated() + { + var hooks = GetRequiredService(); + + await RunInTenantAsync(CompiledQueryCacheTestModuleBase.TenantA, async db => + { + var currentDbContext = db.GetService(); + var contexts = new List(); + hooks.OnGetCompiledQueryCacheKey = () => contexts.Add(currentDbContext.Context); + try + { + db.Rows.Select(row => row.Name).ToList(); + await db.Rows.Select(row => row.Name).ToListAsync(); + } + finally + { + hooks.OnGetCompiledQueryCacheKey = null; + } + + contexts.Count.ShouldBe(2); + contexts.ShouldAllBe(context => context == db); + return new List(); + }); + } + + [Fact] + public async Task Should_Not_Compile_Query_With_Cache_Key_Generated_Outside_Of_Query_Provider() + { + await Should.ThrowAsync(() => + RunInTenantAsync(CompiledQueryCacheTestModuleBase.TenantA, db => + { + var expression = ((IQueryable)db.Rows).Expression; + db.GetService().GenerateCacheKey(expression, false); + db.GetService().CompileQuery>(expression, false); + return Task.FromResult(new List()); + })); + } + + [Fact] + public async Task Should_Apply_Filters_To_Normal_Query_After_Compiled_Query_Failed() + { + var countRows = EF.CompileQuery((CompiledQueryCacheTestDbContext db) => db.Rows.Count()); + + await Should.ThrowAsync(() => + RunInTenantAsync(CompiledQueryCacheTestModuleBase.TenantA, db => + Task.FromResult(db.Rows.Where(row => row.Name.Length > countRows(db)).Select(row => row.Name).ToList()))); + + (await RunInTenantAsync(CompiledQueryCacheTestModuleBase.TenantA, db => + db.Rows.OrderBy(row => row.Name).Select(row => row.Name).ToListAsync())) + .ShouldBe(new[] { "A-live" }); + + (await RunInTenantAsync(CompiledQueryCacheTestModuleBase.TenantA, async db => + { + Should.Throw(() => db.Rows.Where(row => row.Name.Length > countRows(db)).Select(row => row.Name).ToList()); + return await db.Rows.OrderBy(row => row.Name).Select(row => row.Name).ToListAsync(); + })) + .ShouldBe(new[] { "A-live" }); + } + + [Fact] + public async Task Should_Run_Compiled_Query_That_Ignores_Filters() + { + var countAllRows = EF.CompileQuery((CompiledQueryCacheTestDbContext db) => db.Rows.IgnoreQueryFilters().Count()); + + (await RunInTenantAsync(CompiledQueryCacheTestModuleBase.TenantA, db => Task.FromResult(new[] { countAllRows(db).ToString() }.ToList()))) + .ShouldBe(new[] { CompiledQueryCacheTestModuleBase.Rows.Length.ToString() }); + } + + [Fact] + public void Should_Not_Allow_Replacing_Database() + { + var options = new DbContextOptionsBuilder() + .UseSqlite("Data Source=:memory:") + .ReplaceService() + .AddAbpDbContextOptionsExtension() + .Options; + + Should.Throw(() => new CompiledQueryCacheTestDbContext(options).Dispose()); + } + + [Fact] + public async Task Should_Not_Compile_Query_Inside_Another_Query_Without_Filters() + { + var exception = await Should.ThrowAsync(() => + RunInTenantAsync(CompiledQueryCacheTestModuleBase.TenantA, db => + Task.FromResult(db.Rows.Where(row => row.Name.Length > CompileRowsQuery(db)).Select(row => row.Name).ToList()))); + exception.InnerException.ShouldBeOfType(); + } + + private static int CompileRowsQuery(CompiledQueryCacheTestDbContext db) + { + db.GetService().CompileQuery>(((IQueryable)db.Rows).Expression, false); + return 0; + } + [Fact] public async Task Should_Not_Run_Compiled_Async_Query_Without_Filters() { diff --git a/framework/test/Volo.Abp.EntityFrameworkCore.Tests/Volo/Abp/EntityFrameworkCore/DataFiltering/CompiledQueryCache/CompiledQueryCache_WithoutDbFunction_Tests.cs b/framework/test/Volo.Abp.EntityFrameworkCore.Tests/Volo/Abp/EntityFrameworkCore/DataFiltering/CompiledQueryCache/CompiledQueryCache_WithoutDbFunction_Tests.cs index 6fdd232ee6..2eb7a09844 100644 --- a/framework/test/Volo.Abp.EntityFrameworkCore.Tests/Volo/Abp/EntityFrameworkCore/DataFiltering/CompiledQueryCache/CompiledQueryCache_WithoutDbFunction_Tests.cs +++ b/framework/test/Volo.Abp.EntityFrameworkCore.Tests/Volo/Abp/EntityFrameworkCore/DataFiltering/CompiledQueryCache/CompiledQueryCache_WithoutDbFunction_Tests.cs @@ -2,6 +2,8 @@ using System.Linq; using System.Threading.Tasks; using Microsoft.EntityFrameworkCore; using Shouldly; +using Volo.Abp.Data; +using Volo.Abp.MultiTenancy; using Xunit; namespace Volo.Abp.EntityFrameworkCore.DataFiltering.CompiledQueryCache; @@ -10,7 +12,6 @@ public class CompiledQueryCache_WithoutDbFunction_Tests : CompiledQueryCacheTest { protected override bool UseDbFunction => false; - // Without DbFunction the filters are parameters of the compiled query, so they are applied on every run. [Fact] public async Task Should_Apply_Filters_To_Compiled_Queries() { @@ -28,4 +29,30 @@ public class CompiledQueryCache_WithoutDbFunction_Tests : CompiledQueryCacheTest (await RunInTenantAsync(CompiledQueryCacheTestModuleBase.TenantA, db => ToListAsync(asyncQuery(db)))) .ShouldBe(new[] { "A-live" }); } + + [Fact] + public async Task Should_Read_Filter_State_On_Every_Run_Of_Compiled_Query() + { + var query = EF.CompileQuery((CompiledQueryCacheTestDbContext db) => + db.Rows.OrderBy(x => x.Name).Select(x => x.Name)); + var dataFilter = GetRequiredService(); + + (await RunInTenantAsync(CompiledQueryCacheTestModuleBase.TenantA, db => Task.FromResult(query(db).ToList()))) + .ShouldBe(new[] { "A-live" }); + + using (dataFilter.Disable()) + { + (await RunInTenantAsync(CompiledQueryCacheTestModuleBase.TenantA, db => Task.FromResult(query(db).ToList()))) + .ShouldBe(new[] { "A-deleted", "A-live" }); + } + + using (dataFilter.Disable()) + { + (await RunInTenantAsync(CompiledQueryCacheTestModuleBase.TenantA, db => Task.FromResult(query(db).ToList()))) + .ShouldBe(new[] { "A-live", "B-live", "Host-live" }); + } + + (await RunInTenantAsync(CompiledQueryCacheTestModuleBase.TenantA, db => Task.FromResult(query(db).ToList()))) + .ShouldBe(new[] { "A-live" }); + } } diff --git a/framework/test/Volo.Abp.EntityFrameworkCore.Tests/Volo/Abp/EntityFrameworkCore/DependencyInjection/ServiceCollectionDecorateExtensions_Tests.cs b/framework/test/Volo.Abp.EntityFrameworkCore.Tests/Volo/Abp/EntityFrameworkCore/DependencyInjection/ServiceCollectionDecorateExtensions_Tests.cs new file mode 100644 index 0000000000..6ab93249ec --- /dev/null +++ b/framework/test/Volo.Abp.EntityFrameworkCore.Tests/Volo/Abp/EntityFrameworkCore/DependencyInjection/ServiceCollectionDecorateExtensions_Tests.cs @@ -0,0 +1,160 @@ +using System; +using Microsoft.Extensions.DependencyInjection; +using Shouldly; +using Xunit; + +namespace Volo.Abp.EntityFrameworkCore.DependencyInjection; + +public class ServiceCollectionDecorateExtensions_Tests +{ + [Fact] + public void Should_Decorate_Type_Registration() + { + var services = new ServiceCollection(); + services.AddScoped(); + + services.Decorate((_, inner) => new DecorateTestServiceDecorator(inner)); + + using var provider = services.BuildServiceProvider(); + using var scope = provider.CreateScope(); + var service = scope.ServiceProvider.GetRequiredService(); + service.ShouldBeOfType().Inner.ShouldBeOfType(); + scope.ServiceProvider.GetRequiredService().ShouldBeSameAs(service); + } + + [Fact] + public void Should_Decorate_The_Last_Registration() + { + var services = new ServiceCollection(); + services.AddScoped(); + services.AddScoped(); + + services.Decorate((_, inner) => new DecorateTestServiceDecorator(inner)); + + using var provider = services.BuildServiceProvider(); + provider.GetRequiredService() + .ShouldBeOfType().Inner.ShouldBeOfType(); + } + + [Fact] + public void Should_Dispose_Inner_Service_Created_By_Factory() + { + var services = new ServiceCollection(); + services.AddScoped(_ => new DisposableDecorateTestService()); + + services.Decorate((_, inner) => new DecorateTestServiceDecorator(inner)); + + using var provider = services.BuildServiceProvider(); + DisposableDecorateTestService inner; + using (var scope = provider.CreateScope()) + { + inner = scope.ServiceProvider.GetRequiredService() + .ShouldBeOfType().Inner.ShouldBeOfType(); + inner.DisposeCount.ShouldBe(0); + } + + inner.DisposeCount.ShouldBe(1); + } + + [Fact] + public void Should_Not_Dispose_Inner_Service_Registered_As_Instance() + { + var instance = new DisposableDecorateTestService(); + var services = new ServiceCollection(); + services.AddSingleton(instance); + + services.Decorate((_, inner) => new DecorateTestServiceDecorator(inner)); + + using (var provider = services.BuildServiceProvider()) + { + provider.GetRequiredService() + .ShouldBeOfType().Inner.ShouldBeSameAs(instance); + } + + instance.DisposeCount.ShouldBe(0); + } + + [Fact] + public void Should_Not_Allow_Returning_The_Decorated_Service() + { + var services = new ServiceCollection(); + services.AddScoped(); + + services.Decorate((_, inner) => inner); + + using var provider = services.BuildServiceProvider(); + using var scope = provider.CreateScope(); + Should.Throw(() => scope.ServiceProvider.GetRequiredService()); + } + + [Fact] + public void Should_Decorate_Once() + { + var services = new ServiceCollection(); + services.AddScoped(); + + services.Decorate((_, inner) => new DecorateTestServiceDecorator(inner)); + services.Decorate((_, inner) => new DecorateTestServiceDecorator(inner)); + + using var provider = services.BuildServiceProvider(); + provider.GetRequiredService() + .ShouldBeOfType().Inner.ShouldBeOfType(); + } + + [Fact] + public void Should_Decorate_Registration_Added_After_Decoration() + { + var services = new ServiceCollection(); + services.AddScoped(); + services.Decorate((_, inner) => new DecorateTestServiceDecorator(inner)); + services.AddScoped(); + + services.Decorate((_, inner) => new DecorateTestServiceDecorator(inner)); + + using var provider = services.BuildServiceProvider(); + provider.GetRequiredService() + .ShouldBeOfType().Inner.ShouldBeOfType(); + } + + [Fact] + public void Should_Not_Change_Services_Without_Registration() + { + var services = new ServiceCollection(); + + services.Decorate((_, inner) => new DecorateTestServiceDecorator(inner)); + + services.ShouldBeEmpty(); + } + + public interface IDecorateTestService + { + } + + public class DecorateTestService : IDecorateTestService + { + } + + public class OtherDecorateTestService : IDecorateTestService + { + } + + public class DisposableDecorateTestService : IDecorateTestService, IDisposable + { + public int DisposeCount { get; private set; } + + public void Dispose() + { + DisposeCount++; + } + } + + public class DecorateTestServiceDecorator : IDecorateTestService + { + public IDecorateTestService Inner { get; } + + public DecorateTestServiceDecorator(IDecorateTestService inner) + { + Inner = inner; + } + } +} From 494a8c8cb91500610463305f75a5d568a81f51a8 Mon Sep 17 00:00:00 2001 From: maliming Date: Thu, 15 Oct 2026 15:19:37 +0800 Subject: [PATCH 5/5] Simplify the compiled query note in the community article --- .../POST.md | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/docs/en/Community-Articles/2024-07-01-Use_User-Defined_Function_Mapping_For_Global_Filter/POST.md b/docs/en/Community-Articles/2024-07-01-Use_User-Defined_Function_Mapping_For_Global_Filter/POST.md index 12f9105125..c7511bdb95 100644 --- a/docs/en/Community-Articles/2024-07-01-Use_User-Defined_Function_Mapping_For_Global_Filter/POST.md +++ b/docs/en/Community-Articles/2024-07-01-Use_User-Defined_Function_Mapping_For_Global_Filter/POST.md @@ -69,14 +69,15 @@ protected override Expression> CreateFilterExpression()) { // isActive == true return new SqlBinaryExpression( ExpressionType.Equal, isActive, - new SqlConstantExpression(Expression.Constant(true), isActive.TypeMapping ?? boolParam.TypeMapping), + new SqlConstantExpression(Expression.Constant(true), boolParam.TypeMapping), boolParam.Type, boolParam.TypeMapping); } @@ -103,8 +104,6 @@ public override string GetCompiledQueryCacheKey() } ```` -> Throw an exception when `abpEfCoreCurrentDbContext.Context` is `null` instead of treating the filter as disabled. In the latest ABP versions, use `GetRequiredContext()`. See the [Data Filtering](https://abp.io/docs/latest/framework/infrastructure/data-filtering) document for the latest example and the limitations of compiled queries. - After these changes, the SQL generated by the EF Core Global Query Filters will be as follows: Enabling the `IIsActive` filter: