Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Expressions not working with global query filters #33

Open
Seramis opened this issue Feb 21, 2025 · 4 comments
Open

Expressions not working with global query filters #33

Seramis opened this issue Feb 21, 2025 · 4 comments

Comments

@Seramis
Copy link

Seramis commented Feb 21, 2025

I'm trying to use Expressionify together with global query filters and it seems it is not working.

To reproduce:

  1. Create entity Tenant
  2. Create entity User, which has Tenant and TenantId property
  3. Create entity Product, which has User and UserId property
  4. Create Expressionify method public static int GetTenantId(Product p) => p.User.TenantId; into Product entity
  5. In DbContext.OnModelCreating(), add filter: modelBuilder.Entity<Product>().HasQueryFilter(x => x.GetTenantId() == 3);

Exception:

Message: 
System.TypeLoadException : Method 'PrecompileQuery' in type 'Clave.Expressionify.ExpressionableQueryCompiler' from assembly 'Clave.Expressionify, Version=6.6.0.0, Culture=neutral, PublicKeyToken=null' does not have an implementation.

Stack Trace: 
ExpressionifyDbContextOptionsExtension.ApplyServices(IServiceCollection services)
ServiceProviderCache.ApplyServices(IDbContextOptions options, ServiceCollection services)
ServiceProviderCache.<GetOrAdd>g__BuildServiceProvider|4_1(IDbContextOptions _, ValueTuple`2 arguments)
<>c.<GetOrAdd>b__4_0(IDbContextOptions contextOptions, ValueTuple`2 tuples)
ConcurrentDictionary`2.GetOrAdd[TArg](TKey key, Func`3 valueFactory, TArg factoryArgument)
ServiceProviderCache.GetOrAdd(IDbContextOptions options, Boolean providerRequired)
DbContext.get_ContextServices()
DbContext.get_InternalServiceProvider()
IServiceProvider>.get_Instance()
InfrastructureExtensions.GetService(IInfrastructure`1 accessor, Type serviceType)
InfrastructureExtensions.GetService[TService](IInfrastructure`1 accessor)
AccessorExtensions.GetService[TService](IInfrastructure`1 accessor)
DatabaseFacade.get_Dependencies()
IDatabaseFacadeDependenciesAccessor.get_Dependencies()
RelationalDatabaseFacadeExtensions.OpenConnection(DatabaseFacade databaseFacade)
AppDbContext.ctor() line 271
EntityFilterEfTests.CreateContext() line 226
EntityFilterEfTests.Products_TenantNotSet_Returns_AllProducts() line 155
RuntimeMethodHandle.InvokeMethod(Object target, Void** arguments, Signature sig, Boolean isConstructor)
MethodBaseInvoker.InvokeWithNoArgs(Object obj, BindingFlags invokeAttr)
@mariusGundersen
Copy link
Contributor

@mariusGundersen
Copy link
Contributor

Hang on, which version of expressionify are you using? It looks like v6, but you need v9

@Seramis
Copy link
Author

Seramis commented Feb 24, 2025

Hang on, which version of expressionify are you using? It looks like v6, but you need v9

I used the latest available from nuget.org at the time when i made this post. (about 3 days ago)

EDIT: Wait. VS has installed 6.6.0. For some reason, latest version was not the default option. I'll retest!

@Seramis
Copy link
Author

Seramis commented Feb 24, 2025

Sorry for the confusion. The VS had 6.6.0 as a default version. I updated to 9.0.0 and got this error now:

Message: 
System.InvalidOperationException : The LINQ expression 'DbSet<Product>()
    .Where(p => p.GetTenantId() == 3)' could not be translated. Either rewrite the query in a form that can be translated, or switch to client evaluation explicitly by inserting a call to 'AsEnumerable', 'AsAsyncEnumerable', 'ToList', or 'ToListAsync'. See https://go.microsoft.com/fwlink/?linkid=2101038 for more information.

Stack Trace: 
QueryableMethodTranslatingExpressionVisitor.Translate(Expression expression)
QueryCompilationContext.CreateQueryExecutorExpression[TResult](Expression query)
QueryCompilationContext.CreateQueryExecutor[TResult](Expression query)
Database.CompileQuery[TResult](Expression query, Boolean async)
QueryCompiler.CompileQueryCore[TResult](IDatabase database, Expression query, IModel model, Boolean async)
<>c__DisplayClass11_0`1.<ExecuteCore>b__0()
CompiledQueryCache.GetOrAddQuery[TResult](Object cacheKey, Func`1 compiler)
QueryCompiler.ExecuteCore[TResult](Expression query, Boolean async, CancellationToken cancellationToken)
QueryCompiler.Execute[TResult](Expression query)
EntityQueryProvider.Execute[TResult](Expression expression)
EntityQueryable`1.GetEnumerator()
IEnumerable<TEntity>.GetEnumerator()
List`1.ctor(IEnumerable`1 collection)
Enumerable.ToList[TSource](IEnumerable`1 source)
EntityFilterEfTests.Products_TenantNotSet_Returns_AllProducts() line 161
RuntimeMethodHandle.InvokeMethod(Object target, Void** arguments, Signature sig, Boolean isConstructor)
MethodBaseInvoker.InvokeWithNoArgs(Object obj, BindingFlags invokeAttr)

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants