Hooks to intercept and modify queries on the fly at high and low level
We have seen that there are lots of discussion surrounding the generated SQL not being optimized. Entity Framework should have a provision wherein User should be able to change the Generated SQL and use it for further processing through some mechanism
Resetting status to reflect current state of EF Core. Some of this functionality is available to EF6-based applications: only access to store DbExpression trees and DbCommand are available, and there are no plans for adding a LINQ level interception point.
Mohammad M commented
I believe it would be much beneficial if it was possible to intercept queries like, select, where etc.
The important thing for me would be that when intercepting, then the query result should be viewable.
I need this to make security check regarding if the user querying the database also has access right the data he querying.
This is unfortunately not possible now using interception e.g.:
var listener = _context.GetService<DiagnosticSource>();
(listener as DiagnosticListener).SubscribeWithAdapter(new CommandListener());
using commercial tools like postSharp to do this is not the way I want to go.
Arturo Hernandez commented
This is similar to the idea of modifying a generated SQL. Except that it applies to an earlier stage. I can imagine a scenario where a third party may come up with a really good LINQ optimizer.
Add hooks thourought the process from QueryExecuting to QueryExecuted - great place to add auditing/security code
Paul King commented
To expand on this a bit further, this would allow us to do something with an Entity before it is added to the ObjectStateManager, including reject the Entity entirely.
This “QueryStarting” event would allow us to “rewrite” the query by wrapping it in an outer query that adds a filter to the results from the main query.
Doing this at the event level allows this type of filtering to be placed in one place int he code instead of on each query being executed.
Matthieu MEZIL commented
We already can do something like this.
For example, I did it to add Table Valued Functions for example. (http://msmvps.com/blogs/matthieu/archive/2010/07/01/how-to-include-recursion-table-valued-functions-in-linq-to-entities-queries-with-ef4-v2.aspx). However, I think that it should be great to be able to use the EF materializer.