-
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
Check for custom sql translation defined on C# special methods: op_LessThan, op_GreaterThan, etc. #32227
Comments
@voroninp operators such as Can you please provide a bit more context, or post the code that doesn't work without what you're asking for? |
I showed the query which won't work for SQLite database when |
Can you please post a repro for that not working? |
@bricelam do we have an issue for adding translations for DateTimeOffset? I can see #18844 for TimeSpan, but nothing for DTO. |
@roji oops..I assumed string representation saves order of DTO, at least for UTC representation. But ok, problem stays the same if DTO is converted to int. I still cannot define custom SQL for comparison operator on DTO ;-) |
EF indeed does not support overriding comparison operators - or any other kind of operator - with custom logic, only function calls. Allowing custom operator overriding logic would mean introducing hooks in many difference expression types (binary, but also unary, casting?), and a whole new API just to allow users to do this. Given that it's already possible to just add a custom function - as you've done for the workaround - I highly doubt that we'd implement something like this. |
Fair enough. And there's already a pending issue for custom translations for value generators. Btw, is it possible to assign custom translation to entity's instance method/property? For example I have a So I either need a mechanism to substitute PropertyExpression/CallExpression in expression tree, so it gets translated by EF, or provide SQL translation. |
Here's the example: public sealed record RadioProgram(
ushort ChannelId,
[StringLength(MaxLengthOf.ProgramHarmonizedTitle)]
string HarmonizedTitle)
{
public static readonly IReadOnlySet<ProgramType> AdvertisingProgramTypes = new HashSet<ProgramType>
{
ProgramType.Billboard,
ProgramType.AnnouncementFromTheGovernment,
ProgramType.Break,
ProgramType.Audioboard,
}.AsReadOnly();
public int Id { get; private set; }
public required ProgramType ProgramType { get; set; }
public required DateIndex OnDate { get; set; }
public required DateTimeOffset Timestamp { get; set; }
public string? BreakCode() => ProgramType switch
{
_ when IsAdvertising() => HarmonizedTitle,
_ => null,
};
public bool IsAdvertising() => AdvertisingProgramTypes.Contains(ProgramType);
} And I obviously cannot call So I declared in DbContext:
and I can make a call: It would be nice to have a mechanism for automatic (configured) replacement of |
Specifying a custom translation for CLR properties isn't currently possibly (barring messing around with visitors in preprocsesing). This is tracked by #10768. I'll go ahead and close this the above questions were answered and I don't think anything actionable remains. |
No. |
Currently EF Core does not provide an easy way to define custom SQL translations or AST transformations for operations defined on converted types.
For example, SQLite does not support
DateTimeOffset
type and it must be converted toint
orstring
.@roji proposed a workaround, but I think it would be nice, if I could define SQL translation for
op_LessThan
method ofDateTimeOffset
type:So, query
items.Where(i => i.TimeStamp < yesterday)
is properly translated to SQL expression.Right now, I must write it like this:
items.Where(i => SqlTranslation.LessThan(i.TimeStamp, yesterday))
It means EF should make additional step when translating expression tree. When it sees one of comparison expressions it should check whether there's a translation defined for a corresponding method.
The major issue with this behavior would be that properties of the same type can be converted differently (or not converted at all) for various properties. But SQL translation applied to special methods is global. Whereas custom function provides flexibility. However, one must check whether type is converted and what function should be used in LINQ for correct translation.
Thus, it's still better to define translations within value converters.
The text was updated successfully, but these errors were encountered: