-
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
Treat ToString as casting to string in the database #20604
Comments
@fingers10 This should work: .Where(x => ((string)(object)x.Position).Contains("acc")) EF Core then interprets this as a cast to string and generates: SELECT [d].[Id], [d].[FirstName], [d].[LastName], [d].[Position]
FROM [DemoEntity] AS [d]
WHERE CHARINDEX(N'acc', CAST([d].[Position] AS nvarchar(max))) > 0 However, we discussed that it would be useful to allow |
Also see #14205 |
@ajcvickers Thanks for the workaround. It works good and expected. |
I'm attempting to fix this issue. My goal right now is to have the SELECT "p"."PostId", "p"."BlogId", "p"."Content", "p"."Title", "p"."Visibility"
FROM "Posts" AS "p"
WHERE 'pr' = '' OR instr(CASE
WHEN "p"."Visibility" = 0 THEN 'Public'
WHEN "p"."Visibility" = 1 THEN 'Private'
ELSE NULL
END, 'pr') > 0
LIMIT 1 |
@Danevandy99 please separate general translation of ToString() to a database-side cast ( |
@roji Sounds good! @ajcvickers mentioned this in his comment about the casting workaround above about how translating .Tostring() to do a database-side cast could cause unexpected results when compared to a client-side evalutation, but I can see how the CASE/WHEN conversion will require more thought and changes, so I'll make a separate issue for that. |
@Danevandy99 we had a thorough design discussion in the team about this, and came to the following conclusions:
Does that sound OK @Danevandy99? If so, you can merge #33701 and #33706 together (close one and integrate its contents into the other - sorry for the churn), update as per the above, and we'll review. |
@roji That sounds great! I like the default case refraining from translating if there is no value converter or a value converter other than the EnumToStringConverter. I'll merge those PR's together and make the changes to line up with the behavior you outlined above. |
To be clear, when there's no value converter, EF maps the enum to an integer in the database - for that case we'd do the CASE/WHEN. The only case where we should refrain from translating is when a value converter is configured, but it's not the built-in EnumToStringConverter. |
Yes, I stated that incorrectly, but I think we are on the same page. I should've finished my coffee before replying 😆 |
I'm trying to do a contains search on
enum
property in myDbSet
and EF Core 3.1 throws the below errorEntity:
Enum - Position:
DbContext:
When I query the table as follows I'm getting the error
The Position is of type
NVARCHAR(MAX)
in my database.This is not possible? If so please can you help me with explanation?
The text was updated successfully, but these errors were encountered: