Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

HasQueryFilter called once #9809

Closed
sergeushenecz opened this issue Sep 13, 2017 · 2 comments
Closed

HasQueryFilter called once #9809

sergeushenecz opened this issue Sep 13, 2017 · 2 comments
Labels
closed-no-further-action The issue is closed and no further action is planned.

Comments

@sergeushenecz
Copy link

Describe what is not working as expected.

I have the problem with the global filter.
I have the code
modelBuilder.Entity<Report>().HasQueryFilter(TenantDataFilter);
and function

 public Expression<Func<Report, bool>> TenantDataFilter
        {
            get
            {
                var expr = IsModerated.HasValue ?
                    (Expression<Func<Report, bool>>)(x => !x.IsDeleted && x.IsModerated == IsModerated) :
                    (Expression<Func<Report, bool>>)(x => !x.IsDeleted);

                return expr;
            }
        }

How do you do call every query?Do I want to change IsModerated variable in the request?
Exception message:
Stack trace:


### Steps to reproduce
Include a complete code listing (or project/solution) that we can run to reproduce the issue.

Partial code listings, or multiple fragments of code, will slow down our response or cause us to push the issue back to you to provide code to reproduce the issue.

```c#
Console.WriteLine("Hello World!");

Further technical details

EF Core version: (found in project.csproj or packages.config)
Database Provider: (e.g. Microsoft.EntityFrameworkCore.SqlServer)
Operating system:
IDE: (e.g. Visual Studio 2015)

@ajcvickers
Copy link
Member

@sergeu90 The correct way to do this is to write a single expression that handles both cases. This is because the filter itself is part of the model, which is only built once and cached. For example:

modelBuilder
    .Entity<Report>()
    .HasQueryFilter(x =>
        IsModerated != null
            ? !x.IsDeleted && IsModerated == x.IsModerated
            : !x.IsDeleted);

This works by capturing the closure variable IsModified from the context so that each time the query is evaluated it will take the current value of IsModified from the context.

@sergeushenecz
Copy link
Author

sergeushenecz commented Sep 15, 2017

@ajcvickers thanks, this is works

@ajcvickers ajcvickers reopened this Oct 16, 2022
@ajcvickers ajcvickers closed this as not planned Won't fix, can't repro, duplicate, stale Oct 16, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
closed-no-further-action The issue is closed and no further action is planned.
Projects
None yet
Development

No branches or pull requests

2 participants