-
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
Simple query filter breaks simple projection #13517
Comments
@smitpatel to investigate |
It is not query filter which is issue. Assigning to @maumar |
problem is because we optimize The RefEntityId is not null, even when the entity is supposed to be filtered out with query filter. So the check:
doesn't really work, and we still try to materialize the RefEntityDto even though the RefEntity that we want to use has been filtered out. Per discussion with @divega and @smitpatel, we should not apply the PK-FK optimization if we need to access the entity anyway somewhere else in the query. Optimization can save us a join, but if the join is there anyway, it doesn't buy us much. |
Notes from triage:
|
What a triage. Instead of better documentation - did it ocur to you to issue a hotfix that fixes a bug that makes a whole subsystem (i.e. query filters) totally unusable? And it is nice to see that you basically tell all of us to go home and use EF - because a bug that shuts down a whole subsystem is scheduled for - ah, 3.0. Nice. Basically "oh, sorry you rely on query filterrs, maybe in a year we prouce a usable product there". This is a 1.0 feature that does not work. 2.1.5 is the next patch. It should get this into a usable state. At minimum 2.2 should include an emergency fix for this. Otherwise basically EF is it - I do not care about EF Core because now you actually tell me that the new features that make it worthwhile and everyone loves pointing out - do not work and will not get fixed. Any manager going through tirage and handing out reprimands? Bevcause this is the 2nd time this particular bug is either not accepted as bug (hey, fix your data) or - ah, scheudled for some far point in the future, usage and impact on users being irrelevant. I personally can only be happy that I am slowly removing query filters anyway (related to some business logic that makes them jsut the wrong place - i.e. we will filter on the REST side, not the model, as some related objects may be "oiutdated" but in our case then still are visible read only, so general filters do not work). But I can bet other people actually use that and are not happy about a 3.0 schedule. |
Do you have any solution or workaround for this problem ? |
@ajcvickers @smitpatel @maumar I'm working on migrating a very large Fintech app from Net Framework to Net Core and this now completely blocking our company moving forward. Is there any chance you could produce a small fork with the work around or tell me where to remove the optimisation myself so that we can push on? What's the justification for leaving this until version 3, it seems to be pretty core functionality? Many thanks for in advance. |
@armitagemderivitec This is the code that performs the optimization |
@maumar that's great thanks! So I'd just need to fork and remove that block? Then the default would be used instead? |
@armitagemderivitec yes, this will fix the case involving navigations (like in the original example). We will no longer try to convert |
Will calling |
That does seem to fix it. But I would like to add to other peoples sentiment that this is really a critical bug that should probably be patched before 3.0.0 |
@maumar I've pulled EFCore locally and I don't even hit the code you mention above. The places where I am hitting this problem are all left joins where I am using DefaultIfEmpty() and then accessing properties on a potential nullable object. What's the correct form here because the select query does not present a nullable object? |
The code I highlighted only addresses the case where EF is expanding navigation into a JOIN. If you create the joins yourself, and the query involves client evaluation you need to add your own null protection. You can do it using from c in ctx.Customers
join o in ctx.Orders on c.Name equal o.CustomerName into grouping
from o in grouping.DefaultIfEmpty()
select o != null ? (int?)o.InvoiceNumber : null If the query is fully translated, the What you might also be seeing is that one of the result properties is expected to be non-nullable, but it ends up as null. You will get this error for queries like: from c in ctx.Customers
join o in ctx.Orders on c.Name equal o.CustomerName into grouping
from o in grouping.DefaultIfEmpty()
select o.InvoiceNumber result is expected to be of type int, but SQL returns a null value and we cant fit it into the expected result type. |
@maumar
This worked in EF6 even without the null operator. Presumably the nested IQueryable is the cause. Any ideas? |
@armitagemderivitec I filed the new bug - #14773 since this looks like a different issue, no QueryFilters seem to be involved in the scenario. |
Its the manager who decides whether it is useful for marketing or not, problem is EF team has never taken problems seriously but are always busy in introducing new in built features of SQL Server or now Cosmos DB in EF Core. The whole focus is on Cosmos DB and all bugs are scheduled for future !! Open source term is misleading here, there is no community and there is no control over what will happen next. |
I didn't have this issue in .NET Core 2.2, Now I'm using .NET Core 3.0 and I'm having trouble with this when using a QueryFilter. I agree that complex filtering queries should not be run client-side but rather with SQL, which is way faster for large datasets. But if the QueryFilter contains a simple expression (which it should), this still can be converted to SQL (can it?) and there's no problem...
And just fetching an entity by id already fails:
I'm a little troubled by this, because since some developers are using bad practices (yielding results from a potentially large DbSet before executing a linq query), now a decision is made that even affects the situation described above. However a collegue at work told me this could be happening while you're not aware of it... But anyway, even the code above isn't working because of this. |
@MusicDemons - Please file a new issue with repro code which we can run. Your issue does not seem to be related to this issue. |
Given the state of EfCore query execution that is not bad practice - is it an approach that works. I do the same. I route ALL queries through a helper method. The helper method does use analyisis of the query parameters and the LINQ nodes to decide whether or not to offload the query to SQL. If not - all data is laoded and evaluated in memory. Bad practice? NO. It is a workaround until I either get so tired to rip out EfCore, or they fix the bugs and I slowly take out cases where I need to evaluate locally. |
@NetTecture you're clearly frustrated with the state of things, but can you please refrain from posting comments that aren't relevant to the issue? The team is working extremely hard to address issues to the best of our abilities, and this doesn't help us make progress. |
As reported by @klaussj here: #12951 (comment)
Removing the query filter fixes the issue.
Exception:
The text was updated successfully, but these errors were encountered: