Skip to content

Add EF.Parameter to force parameterization in query #28151

Description

@roji

In F#, closure parameters are integrated into the query expression tree as constant nodes - see dotnet/fsharp#12841 for the full details. This means that by default, all EF queries generate SQL with constants rather than parameters.

A workaround is to use a F# ref, which gets properly parameterized by EF Core:

let i = ref 8
db.Events.Where(fun x -> x.Data = i.Value).ToList();

However, while this works well with local variables, it's problematic for use with function parameters, since the parameter needs to be typed as Ref<T> (it isn't great to require people to change method signatures just so that an EF query inside get properly parameterized). Note that the variable name is also lost, so the SQL contains @__Value_0.

We could introduced an EF.Param() mechanism, which is identified by EF Core and forces parameterization. The same mechanism could be used in C# to force parameterization for better perf when two queries use different constants, without needing to extract a variable:

var query1 = await ctx.Blogs.Where(b => b.Name == EF.Param("foo")).ToListAsync();
...
var query2 = await ctx.Blogs.Where(b => b.Name == EF.Param("bar")).ToListAsync();

EF.Param could optionally also accept the SQL parameter name, to produce more readable SQL queries.

/cc @dsyme @NinoFloris

Activity

  1. roji commented on Jun 3, 2022

    @roji
    MemberAuthor

    Additional advantage of this: for users building an expression tree dynamically, it would make it considerably easier to integrate a value as a parameter into the expression tree.

  2. smitpatel commented on Jun 3, 2022

    @smitpatel
    Contributor

    Restriction on the API would be that the argument to the method must be a constant value. (this also works in dynamic tree building scenario where user generally creates ConstantExpression).

    We would also evaluate anything which composes over as long as it is evaluate-able using normal rules of maximum possible evaluation.

    e.g. EF.Param("foo").Length would get fully evaluated and generated parameter of value 3 in expression tree.

  3. added this to the Backlog milestone on Jun 6, 2022
  4. pinkfloydx33 commented on Jan 7, 2023

    @pinkfloydx33

    This would be really useful for us. We have a bunch of query building methods that dynamically craft an expression tree. We were ending up with constant values plugged directly into the query because we were using Expression.Constant. As a workaround I use the following helper method to force a closure over the value(s) passed into our builders:

    public static Expression CloseOver<T>(T constant)
       => ((Expression<Func<T>>)(() => constant)).Body;

    Which we then use in lieu of Expression.Constant when dealing with various input values. So far this seems to work w/r/t parametrization, however we end up with parameters all named constant_{n} (the name of the parameter/display class field) which is a bit unfortunate. I'm also not sure if it's guaranteed to work or not and it feels a tad bit hacky.

    It'd be great if we had some built-in method we could Expression.Call instead that would properly recognize this and turn the inputs into parameters named accordingly.

  5. roji commented on Jan 7, 2023

    @roji
    MemberAuthor

    @pinkfloydx33 your workaround above is fine and guaranteed to work - it just reproduces the normal non-dynamic thing that happens when a closure variable is captured. But it's true that the parameter name isn't pretty.

  6. roji commented on Aug 31, 2023

    @roji
    MemberAuthor

    Do this together with #31552, which does the opposite, adding EF.Constant to force constantization.

  7. roji commented on Nov 27, 2023

    @roji
    MemberAuthor

    Thinink about #28151 (comment):

    We would also evaluate anything which composes over as long as it is evaluate-able using normal rules of maximum possible evaluation.

    e.g. EF.Param("foo").Length would get fully evaluated and generated parameter of value 3 in expression tree

    Evaluating operators composed over EF.Param seems unnecessary, since they'd get evaluated anyway without it; in other words, p.Length already gets fully parameterized, so there's no reason to use EF.Param() here in the first place.

    There might conceivably also be cases where a user may want to block evaluation at a higher level, i.e. ensuring that some part of the tree is sent as a parameter, and not subsumed into a larger parameter (as contrived as it sounds, this may be helpful to optimize certain SQL queries).

    (plus it's a bit more difficult implementation-wise to allow EF.Param() to be evaluated within a larger subtree)

  8. added a commit that references this issue on Nov 27, 2023
    1f50fa8
  9. modified the milestones: Backlog, 9.0.0 on Nov 27, 2023
  10. self-assigned this
    on Nov 27, 2023
  11. changed the title [-]Consider adding EF.Param to force parameterization in query[/-] [+]Add EF.Parameter to force parameterization in query[/+] on Nov 27, 2023
  12. added a commit that references this issue on Nov 28, 2023
    7ed714a
  13. added a commit that references this issue on Nov 28, 2023
    d49e211
  14. kjeske commented on Jan 4, 2024

    @kjeske

    I really like it and thank you for it. But do we need to wait till November 2024 to have it in non-prerelease version?

  15. roji commented on Jan 4, 2024

    @roji
    MemberAuthor

    @kjeske yes.

  16. modified the milestones: 9.0.0, on Jan 31, 2024
  17. modified the milestones: , 9.0.0 on Oct 12, 2024
  18. added theissue type on Jun 15, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions