-
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
Allow global query filters to be defined on a derived Entity Type of a hierarchy #10259
Comments
@HappyNomad Everything is always open to change based on feedback. It was emphasized for navigation references because we explicitly wanted to elicit feedback on that one since we wanted to see what kinds of ways people wanted to use navigation properties so that we could look at how difficult it made things with this limitation in place. This would in turn drive the priority of adding support. For inheritance, the implementation was investigated and it was quite complex. At the time we felt that the value would not out-weigh the cost, but if it is something that enough people want, then we could reconsider this. Leaving this open so we can discuss the priority again in triage. |
@ajcvickers @HappyNomad I used the syntax like you suggested as a workaround and it seems to be working ok for me with a relatively small dataset. |
I am getting the same problem with this scenario. |
+1 It would be great to have the ability to write EF queries that filter based on inheritance. My use case is simple, I have a hierarchy of log entities that inherit from a base log entity. The discriminators are success, error, info, diagnostics, etc. I'd like to write a single query against the base type and filter it so I get all but Diagnostics back. Pseudo code below.
|
@reloaded - Since you want to filter on base type, you should be able to do that even with current functionality. |
Could you advise me on how to do this in a way that does the filtering in SQL? When I attempted to achieve this type filtering by using the type "Is" operator was not translated to SQL so LINQ ran the filter in the app. Here's the code I was using before I gave up and wrote 4 separate LINQ queries. The requirements are to count how many logs of each type there are where ImportOperationId is equal to some ID that identifies the group of logs.
|
public class Blog
{
public int Id { get; set; }
}
public class SpecialBlog :Blog
{
}
// Query Filter
modelBuilder.Entity<Blog>().HasQueryFilter(e => !(e is SpecialBlog)); // Query
var query = db.Blogs.ToList();
// Generates SQL
SELECT [e].[Id], [e].[Discriminator]
FROM [Blogs] AS [e]
WHERE [e].[Discriminator] IN (N'SpecialBlog', N'Blog') AND NOT ([e].[Discriminator] = N'SpecialBlog') The filter you want to apply is working for me. I am not sure why are you facing issue with it. Please file a new issue with detailed repro for the issue you are seeing. |
Interesting, so it looks like you're using the EF model builder API in ApplicationDbContext to ensure the said filter is applied to the entire DbSet. Is there a way to achieve this functionality without baking it into the EF Model? Ideally I want to write multiple repository methods, each querying against the same EF DbSet, but each repository method can optionally filter the query further based on the discriminator. There will be REST APIs that pull all the logs down and some pull non-diagnostic level logs. It seems tedious that I would have to create several DbSet in the DbContext just to achieve this simple filtering functionality. |
@reloaded Is there a reason you want to use global query filters for that instead of adding filters as needed locally to the queries that should have the filters? |
Global query filters seem to be the method I'm trying to avoid. I don't want my logging DbSet to always have diagnostics filtered out. My goal is to achieve this class type filtering for only one or two queries. |
Hi, I m looking for the same functionality, a way to filter like you proposed @reloaded : In my situation, I have a list of item to display, the user can filter by one or multiple Type. |
Ok, guys, I have found a working solution. Say thanks to these guys: |
Nice read. I had abandoned hope and went in a different direction on this but the article gave me some new ideas and reminded me how much I liked this as a feature. Thx |
Came here for the same... Leaving sad knowing that after 3 years there's still this lack of functionality. My case is very simple: I have |
I ran into this issue as well, and ended up with one separate query pr entity :(
Actually when working with derived types in OData it tries to execute the above |
Hello, Is there a workaround for this?
Thank you! |
|
Is there a reliable solution for this use case? I've encountered the same issue. |
Sad, that no solution is out yet. It is quite a common scenario... :( |
The What's New doc says,
The first limitation says "This feature may be added based on feedback.", and I found #8881 covers it. The second limitation doesn't say that. Does that mean that limitation will likely never be removed?
It seems I could partially get around the second limitation by writing:
Will this work? (I haven't been able to confirm yet due to unrelated strange errors.) What about accessing a property on the subclass like the following?
When I tried this, I get the error, "No coercion operator is defined between types" where one type isn't even used in the expression. It would be great if it does work, but removing the second limitation would allow nicer syntax.
The text was updated successfully, but these errors were encountered: