-
Notifications
You must be signed in to change notification settings - Fork 3.2k
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
Count() is not working correctly with Include() and HasQueryFilter() #29599
Comments
_dbContext.Activities.Include(a => a.Customer).Count(); returns a number of entities in Activities - the include has no influence on that number and is therefore optimized out. The second query, i.e.: _dbContext.Activities.Include(a => a.Customer).ToList(); is the one that returns unexpected results from EF perspective - see https://learn.microsoft.com/en-us/ef/core/querying/filters#accessing-entity-with-query-filter-using-required-navigation for more info. Instead, you can define a query filter on Activity: modelBuilder.Entity<Activity>().HasQueryFilter(a => !a.Customer.IsDeleted); this way activities whose customer is deleted will be filtered out in all cases. |
Hi @maumar , Thank you for your response. I already read your shared link before. I do not suppose I understand clearly. Because there is a sentence like below in your shared link.
And you now say "Global query filters always may not be added". I think there is an inconsistency in here.
Finally, about in your suggestion. I saw this method. it worked. But I think, this solution is not sustainable. Because I have to think every combinations between entities while implementing this. And then maybe the queries will run too slow. Do you have any play to fix it. Or I think your recommendation is that we should not use EF Core and LINQ in enterprise application because of this situation. |
The same conversation is happening in #19649 (comment), this really is a duplicate. |
Duplicate of #19649 |
You wanted me to open new issue. And then I opened this. |
@hakanaltindis you're right, that's my fault - I initially understand your comment on #19649 as a new, unrelated issue rather than continuing discussion of the original design. I'll go ahead and close this, and we can continue on that issue. |
Hi everybody,
Count() method in EF Core is not working correctly when I called one entity what has global query filter with Include() method.
And consider records of the tables in database like the below
Customer
Activity
I configured everything well.
When I called like the below
Query is like that:
If I call like that:
The query is like that:
What can I do to solve this problem?
Include provider and version information
EF Core version:
Database provider: Npgsql.EntityFrameworkCore.PostgreSQL
Target framework: .NET 6.0
Operating system: Windows 10
IDE: Visual Studio 2022 17.4.1
The text was updated successfully, but these errors were encountered: