efcore icon indicating copy to clipboard operation
efcore copied to clipboard

Precompiled queries: support value converters

Open roji opened this issue 1 year ago • 1 comments

We have a general problem with the "quotability" of type mappings. That is, when a type mapping is encountered somewhere when precompiling a query, we need to be able to generate code that finds/creates the same type mapping when the program actually starts up. In the shaper, in most cases we know the type mapping's property, so we can generate a lookup for that property on the model, and get the type mapping from there. But when generating the SQL tree (or the TypeMappedRelationalParameter directly, when pregenerating SQL), we don't have the property, since nodes in the SQL tree only reference the type mapping directly.

At the moment, we're faking it by generating code that simply instantiates the type mapping given the various facets (store type, size...); but this doesn't work for value converters (and comparers). As a short-term solution, we should be able to pass in the value converter directly as a lambda, assuming that lambda is generatable as C# (i.e. doesn't capture some external state). But long-term, we need a more general mechanism for type mapping quotability, where e.g. the type mapping would reference its originating property, so that we can generate a model lookup for that.

/cc @ajcvickers

roji avatar Apr 15 '24 18:04 roji

related: https://github.com/dotnet/efcore/issues/33517

maumar avatar Apr 15 '24 22:04 maumar