You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Extending with provider-specific LINQ operators (like how linq2db allows a wide variety of SQL constructs)
More flexible and robust query filters
Query validation and transformation
The current query generation pipeline is hard to extend without relying on implementation details.
Proposal
Enable intercepting the translation steps in QueryCompilationContext.CreateQueryExecutor.
API details are to be discussed here, I don't have a concrete proposal yet.
Reacted by Ivan Stoev, Matt deClercq, Arad Alvand, cocowalla, sh03, Martin Zima, Svyatoslav Danyliv, Joakim Riedel, Greg Bachraty, Eli Davidson and 20 more
@BalassaMarton the query pipeline is already extensible in many different ways, thanks to the DI infrastructure of EF Core:
Providers (and plugins) can easily add method and member translators
Providers (and even users) can provide their own implementation of IRelationalSqlTranslatingExpressionVisitorFactory to translate other types of expressions
Providers (and even users) can add query pipeline preprocessors (which run at the very beginning) and postprocessors (which run after translations). We do have plans to improve the API for inserting new preprocessing/postprocessing visitors for 5.0.
Admittedly we currently lack documentation on how to do the above - we definitely plan on making all this more discoverable for 5.0 (see dotnet/EntityFramework.Docs#681 for example, as well as other issues).
Are there any specific scenarios you have in mind, where you're unsure how to proceed? Is your interest more as a provider writer, or as an end user?
Note from team discussion: we need to document this (see dotnet/EntityFramework.Docs#951 and dotnet/EntityFramework.Docs#500) but following that we should look at adding sugar for common patterns to avoid the need to understand expression visitors for common custom translations.
@BalassaMarton & @powermetal63 - Can you provide details of exactly what you want to change in expression tree for which you need extensibility? We can determine based on that what could be right hook for it and if it is missing, add it. Query pipeline is a complex system which has multiple parts end to end. As @roji mentioned above, there are hooks a long the way depending what kind of task need to be done.
@smitpatel I would start with the ability to preprocess/transform the IQueryable.Expression before EF Core infrastructure. This will be very useful for lambda injection extensions like NeinLinq, LinqKit and similar which currently have to implement custom IQueryProvider and IQueryable just to do that. The end user is required to call a custom method like ToInjectable(), AsExpandanle(), which can easily be forgotten, and also custom query providers doesn't play well with EF Core queryable/async extensions.
Also it will be useful if we want to add "fixes" to some things you are not willing to (or have no time, or will do it in future version we cannot afford waiting for etc.), like the one here #19930 (comment).
I guess all this falls into bullet #3 - Easy way to add query pipeline preprocessors. By easy I mean to be able to do that without implementing custom options in order to add services etc. Something as easy to use as the current ReplaceService.
@smitpatel But we don't want it do be done externally. I already gave you several examples. We don't want to ask the user to call custom methods like ToInjectable, AsExpandable, ConvertGroupJoins etc. in order get the functionality. Because we are extending/fixing EF/C# query limitations, and want these pure query expression transformations to be executed automatically for each EF query "compilation".
So what we want is an easy way to plug something similar to your current QueryTranslationPreprocessor.Process - like GroupJoinConverter from #19930 (comment), or LinqKit Expand functionality. Or in other words, something similar to AddInterceptors, but for query preprocessors which then run before EF.
@powermetal63 - You can already replace QueryTranslationPreprocessor by using ReplaceService API on IQueryTranslationPreprocessorFactory service. Did you look at that functionality?
@powermetal63 - You can already replace QueryTranslationPreprocessor by using ReplaceService API on IQueryTranslationPreprocessorFactory service. Did you look at that functionality?
@smitpatel Sure I know it, but that's the problem - all I can do is to replace it. Let say I replace it and return custom QueryTranslationPreprocessor. Which one QueryTranslationPreprocessor should I inherit - QueryTranslationPreprocessor, RelationalQueryTranslationPreprocessor or something else - at that moment I have no idea which is the default (current) IQueryTranslationPreprocessorFactory, what dependencies it has and what type of QueryTranslationPreprocessor. it creates. And since EF relies on that query preprocessing, we can easily break it unintentionally.
Shortly, IMHO this factory service approach is quite limited - I'm not sure why you created it and what it will be used for internally by EF, but it definitely is not something I (or 3rd party extensions creators) want to dial with. We don't need to replace something existing, we need something additive like recently added interceptors.
P.S. Please don't get me wrong. I'm doing EF Core hacks/workarounds from the very beginning. Here we are discussing a good public way of extending EF Core. Cheers.
I am currently trying to use the ReplaceService solution in order to register additional functions when evaluating expressions. This seems to work fine when using a real microsoft wql connection. However in our unit tests we use an in memory database. Whenever I try to resolve e.g. QueryTranslationPreprocessorDependencies this fails. Is there some sample code on how this would work?
There is a lot of back-and-forth about the fact that this can be done, but not a whole lot of info on how to actually do it. Especially considering it is still undocumented.
Can anyone shed some light on how I'm supposed to implement this?:
If you are using SqlServer or Sqlite provider, then derive from RelationalQueryTranslationPreprocessor add your custom processor. Create a new implementation of IQueryTranslationPreprocessorFactory which during create method creates the new derived class you generated and use Replace service method to register your custom factory in EF Core provider and it works.
I understand replacing the service with my own factory, but how do I actually derive from RelationalQueryTranslationPreprocessor?
public class CustomQueryTranslationFactory : IQueryTranslationPreprocessorFactory
{
public QueryTranslationPreprocessor Create(QueryCompilationContext queryCompilationContext)
{
return new MyCustomPreprocessor(queryCompilationContext);
}
}
public class MyCustomPreprocessor : RelationalQueryTranslationPreprocessor
{
public MyCustomPreprocessor(QueryCompilationContext queryCompilationContext)
//What the heck are the first two arguments to this constructor supposed to be?
: base(null, null, queryCompilationContext)
{
}
public override Expression Process(Expression query)
{
return base.Process(query);
}
}
I could also be misunderstanding what this is for. We used IDbCommandTreeInterceptor extensively in EF6 and it was quite simple to transform the DbCommandTree before it was translated into SQL. Is this issue proposing this level of control or am I barking up the wrong tree?
@MrZander if your goal is to perform some transformation on the incoming LINQ expression tree - that's the tree that represents your LINQ query before it's translated to SQL, then we've added an interceptor for that in EF Core 7.0 (docs). I'm mentioning that since that's what preprocessing means in this context. So it should no longer be necessary to extend RelationalQueryTranslationPreprocessor.
This issue is about intercepting at a much later point in query processing, after it has already been translated to SQL; so the interceptor here would accept an expression tree representing a (mostly) finalized representation of the SQL query, just before it gets serialized to a string.
My goal is the latter.
Ideally, we would be able to intercept the SqlExpression as close to the end of the pipeline as possible.
I see a few services I could potentially use, like IMemberTranslator or ISqlExpressionFactory, but it isn't clear to me which will get me the "final" representation of the SqlExpression.
For some context, we are trying to swap out tables and add filtering at runtime in order to show entities at a certain point-in-time. The replacement tables are not part of the entity model, so I can't reference them in the LINQ query.
Yeah, at this point you best bet is to use a command interceptor to do this at the SQL string level. It's definitely not great - but that's the recommended thing at the moment.
Otherwise, you would replace RelationalQueryTranslationPostprocessor (postprocessor, not preprocessor), and add an additional visitation step at the end that would go through the SQL tree and do its thing. This is not a trivial thing to do, but should be possible.
Activity
roji commentedon Jan 30, 2020
@BalassaMarton the query pipeline is already extensible in many different ways, thanks to the DI infrastructure of EF Core:
Admittedly we currently lack documentation on how to do the above - we definitely plan on making all this more discoverable for 5.0 (see dotnet/EntityFramework.Docs#681 for example, as well as other issues).
Are there any specific scenarios you have in mind, where you're unsure how to proceed? Is your interest more as a provider writer, or as an end user?
BalassaMarton commentedon Jan 31, 2020
I'm only interested as a user, but I'll look deeper into the visitors and try to understand how it can be extended, thank you.
ajcvickers commentedon Feb 3, 2020
Note from team discussion: we need to document this (see dotnet/EntityFramework.Docs#951 and dotnet/EntityFramework.Docs#500) but following that we should look at adding sugar for common patterns to avoid the need to understand expression visitors for common custom translations.
smitpatel commentedon May 7, 2020
@BalassaMarton & @powermetal63 - Can you provide details of exactly what you want to change in expression tree for which you need extensibility? We can determine based on that what could be right hook for it and if it is missing, add it. Query pipeline is a complex system which has multiple parts end to end. As @roji mentioned above, there are hooks a long the way depending what kind of task need to be done.
powermetal63 commentedon May 7, 2020
@smitpatel I would start with the ability to preprocess/transform the
IQueryable.Expressionbefore EF Core infrastructure. This will be very useful for lambda injection extensions like NeinLinq, LinqKit and similar which currently have to implement customIQueryProviderandIQueryablejust to do that. The end user is required to call a custom method likeToInjectable(),AsExpandanle(), which can easily be forgotten, and also custom query providers doesn't play well with EF Core queryable/async extensions.Also it will be useful if we want to add "fixes" to some things you are not willing to (or have no time, or will do it in future version we cannot afford waiting for etc.), like the one here #19930 (comment).
I guess all this falls into bullet #3 - Easy way to add query pipeline preprocessors. By easy I mean to be able to do that without implementing custom options in order to add services etc. Something as easy to use as the current
ReplaceService.smitpatel commentedon May 7, 2020
That already works for everything you want to inject during compilation phase.
Anything before pre-funcletization can be done outside of EF before passing expression into EF.
powermetal63 commentedon May 8, 2020
@smitpatel But we don't want it do be done externally. I already gave you several examples. We don't want to ask the user to call custom methods like
ToInjectable,AsExpandable,ConvertGroupJoinsetc. in order get the functionality. Because we are extending/fixing EF/C# query limitations, and want these pure query expression transformations to be executed automatically for each EF query "compilation".So what we want is an easy way to plug something similar to your current
QueryTranslationPreprocessor.Process- likeGroupJoinConverterfrom #19930 (comment), or LinqKitExpandfunctionality. Or in other words, something similar toAddInterceptors, but for query preprocessors which then run before EF.smitpatel commentedon May 8, 2020
@powermetal63 - You can already replace QueryTranslationPreprocessor by using ReplaceService API on
IQueryTranslationPreprocessorFactoryservice. Did you look at that functionality?powermetal63 commentedon May 8, 2020
@smitpatel Sure I know it, but that's the problem - all I can do is to replace it. Let say I replace it and return custom
QueryTranslationPreprocessor. Which oneQueryTranslationPreprocessorshould I inherit -QueryTranslationPreprocessor,RelationalQueryTranslationPreprocessoror something else - at that moment I have no idea which is the default (current)IQueryTranslationPreprocessorFactory, what dependencies it has and what type ofQueryTranslationPreprocessor. it creates. And since EF relies on that query preprocessing, we can easily break it unintentionally.Shortly, IMHO this factory service approach is quite limited - I'm not sure why you created it and what it will be used for internally by EF, but it definitely is not something I (or 3rd party extensions creators) want to dial with. We don't need to replace something existing, we need something additive like recently added interceptors.
P.S. Please don't get me wrong. I'm doing EF Core hacks/workarounds from the very beginning. Here we are discussing a good public way of extending EF Core. Cheers.
smitpatel commentedon May 8, 2020
Infrastructure to extend EF already exists as asked by OP. It is personal preference to use it or not. That has no affect on this issue whatsoever.
28 remaining items
ajcvickers commentedon Jul 25, 2022
Note: #28505 to allow interception of the LINQ expression tree has been split off from this and implemented in EF7.
[-]Interceptors for expression trees[/-][+]Interceptors for SQL generation[/+]sensslen commentedon Sep 6, 2022
I am currently trying to use the ReplaceService solution in order to register additional functions when evaluating expressions. This seems to work fine when using a real microsoft wql connection. However in our unit tests we use an in memory database. Whenever I try to resolve e.g. QueryTranslationPreprocessorDependencies this fails. Is there some sample code on how this would work?
MrZander commentedon Mar 13, 2024
There is a lot of back-and-forth about the fact that this can be done, but not a whole lot of info on how to actually do it. Especially considering it is still undocumented.
Can anyone shed some light on how I'm supposed to implement this?:
I understand replacing the service with my own factory, but how do I actually derive from
RelationalQueryTranslationPreprocessor?I could also be misunderstanding what this is for. We used
IDbCommandTreeInterceptorextensively in EF6 and it was quite simple to transform theDbCommandTreebefore it was translated into SQL. Is this issue proposing this level of control or am I barking up the wrong tree?roji commentedon Mar 13, 2024
@MrZander if your goal is to perform some transformation on the incoming LINQ expression tree - that's the tree that represents your LINQ query before it's translated to SQL, then we've added an interceptor for that in EF Core 7.0 (docs). I'm mentioning that since that's what preprocessing means in this context. So it should no longer be necessary to extend RelationalQueryTranslationPreprocessor.
This issue is about intercepting at a much later point in query processing, after it has already been translated to SQL; so the interceptor here would accept an expression tree representing a (mostly) finalized representation of the SQL query, just before it gets serialized to a string.
MrZander commentedon Mar 14, 2024
My goal is the latter.
Ideally, we would be able to intercept the SqlExpression as close to the end of the pipeline as possible.
I see a few services I could potentially use, like
IMemberTranslatororISqlExpressionFactory, but it isn't clear to me which will get me the "final" representation of theSqlExpression.For some context, we are trying to swap out tables and add filtering at runtime in order to show entities at a certain point-in-time. The replacement tables are not part of the entity model, so I can't reference them in the LINQ query.
roji commentedon Mar 14, 2024
Yeah, at this point you best bet is to use a command interceptor to do this at the SQL string level. It's definitely not great - but that's the recommended thing at the moment.
Otherwise, you would replace RelationalQueryTranslationPostprocessor (postprocessor, not preprocessor), and add an additional visitation step at the end that would go through the SQL tree and do its thing. This is not a trivial thing to do, but should be possible.