-
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
Including entity with a query filter can cause not loading the root entity #11691
Comments
@jirikanda We discussed this in triage and decided we needed some more information on what you are trying to do. It seems like the case is this:
In the case where there is no Include, this results in a Child being returned even though there its Master would never be returned. This basically violates the relationship defined between Child and Master. In the case where the Include is used, the results could be considered correct, because the filter says that that Master should not be returned, and hence by the definition of the relationship there cannot be any Child with a reference to it. So, it seems like the problem here is that the combination of a filter on the Master and a query directly against a Child bypassing the Master is resulting in the data not matching what the model says. At least, that's one interpretation. We would like to know how you are thinking about the scenario so that we can try to compare our expectations to yours and decide on a plan of action. |
Thank you very much for the answer. My description contains a synthetic scenario, that’s why Child/Master classes are not well understandable. I will describe it in more details.
There is a conflict in our goal (we never filter out references, but we USUALLY filter out collections), but it fits our practice and business requirements. That's our interpretation of soft delete ;-). I tried the behavior of the Entity Framework Core and I did not expect the Include method should limit the root objects. Furthermore, I did not notice such information in the documentation (https://docs.microsoft.com/en-us/ef/core/querying/related-data). |
@jirikanda Thanks for the detailed description. This is very useful. |
Triage: we will fix this post 2.1 by updating the query generation to not use inner join in this case--that is, when principal has a filter. @jirikanda For now, if you make the relationship optional, then it should use a left join. You might also look into using a filter that uses a navigation property to make the behavior consistent between the Include and no Include case--see #8881 |
Could this be linked to issue #8576? |
Not sure if you need more feedback but I have the exact same use case as @jirikanda. Changing the relationship as optional is not really a good workaround IMO as it would require the FK field to be made nullable and I don't really want to change the DB schema forth and back because of this (also people would be confused by the nullable field). I'll rather disable the query filter for the given entity type or maybe ignore on per-use basis. |
Because of this behavior we do not use query filters at all. |
I have the same problem as @jirikanda and a similar use case. In my case I have an Archived property on the parent entity. We have a view where we use IgnoreQueryFilters to explicitly view Archived parents. When we drill into these records we then cannot view the child records because we are including some information from the parent and so they get filtered out because they are archived. We don't want to IgnoreQueryFilters on the child collection to get around this because we may add additional Global Filters on the child collection. (In fact, side point, it would be helpful to be able to key global filters so that we ignore them individually , e.g. |
@smitpatel Could you give some more information on how this is fixed? I have the exact same usecase as @jirikanda .
We upgraded to 3.1 but the entity is still not loaded. Is there now an option on the .Include method to ignore the query filter for this specific include? Are we missing something else? |
related issue: #19649 |
@jgoyvaerts - File a new issue with repro code please. |
@smitpatel - I'm facing this exact issue now in 3.1 also. We really need a way to control query filters on a per-Include basis. For example, assuming all entity types have a soft-delete query filter defined, this would be ideal:
In this example, the join onto Manufacturer would not contain the query filter condition. Is there any other way to achieve this behaviour at present? |
After going through all the linked issues here
Looking at this issue all the related issues,
Overall, global query filters are filters which will be applied to DbSet whenever querying for certain entity type. Further, referential integrity constraint should match model, with & without filter because model metadata is used to generate appropriate join. None of above scenarios are actual use case for global query filters.
Folks above who are facing issue or anyone new who arrives here with similar issue, consider using a different solution than global query filters. If the data cannot conform to model configuration about a relationship with & without query filter then it is not supported scenario with query filter. Consider using different refactoring, including manual joins for your case. Marking this as closed by design. |
In a DB First scenario, would there be any unexpected side effects in modelling non-null int FKs in the DB as nullable int properties in EF, so that the relationship was considered optional and LEFT JOINs used, getting around issues where filters are used? If using ClientNoAction delete behaviour, no attempts to null the FK when deleting entities. We would mandate/validate the true ‘requiredness’ in our create/update DTOs. Anything else to consider? —- I really do think this ‘strict’ approach you’re taking to global filters is unnecessarily complicating it. Like, EF isn’t actually the database so why can’t we use extremely useful features built into EF, like query filters, to make querying the database easier? Being able to rely on navigation properties instead of manual join syntax everywhere makes life so much easier, means DTOs can be projected with AutoMapper with easy to reuse code (query TEntity, project to TDto). Having to rewrite to use custom queries everywhere for each specific TEntity to do the manual joins is a pain- feels a step closer to just having to write the SQL. I think maybe there is too much focus on ‘soft delete’ for which maybe there is an argument should conform to strict database referential integrity. (But even then- it doesn’t have to, does it? That’s why it’s soft and not an actual delete.) However, query filters could actually be used for anything; perhaps forgetting soft delete might help reason about this. |
Lots of emails and notifications with these reopenings… |
@stevendarby We're moving non-fixed issues from closed as "complete" to closed as "not planned". The notifications are unfortunate. |
@ajcvickers ah ok, yeah, though I thought I’d seen direct “closed as complete” to “closed as not planned” before without the reopening between - which is the bit I’m getting notified about. But anyway, no big deal! |
The API returns an error trying to do it without opening first. |
I would expect the following code returns the instance of Child (the same instances).
But it depends on a query filter. If child's master is filtered, the second line returns null.
Steps to reproduce
Further technical details
EF Core version: 2.0.2
Database Provider: Microsoft.EntityFrameworkCore.SqlServer
Target .NET Framework: 4.6.1
The text was updated successfully, but these errors were encountered: