-
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
Allow query operations on the underlying type when a value converter is defined #30197
Comments
@glen-84 Please post the full stack trace. |
Apologies.
|
@ajcvickers Do you know if this is a bug or a missing feature? I opened it as a question because I wasn't sure. It's quite a big issue for us because we are making quite extensive use of value objects. |
@glen-84 Do you have operator overloads defined? Because otherwise this won't compile. It would be a good idea to attach a small, runnable project or post a small, runnable code listing that reproduces what you are seeing so that we can investigate. In the general case, this would require #10434. In simple cases the following can work: context.Articles.Where(a => a.Title == (object)title) But without understanding more about your specific case I don't know if this will help. |
Yes, we have operator overloads defined, and the project does compile. It is a runtime issue. Simple reproduction here. |
Note for triage: this is a case where the @glen-84 An explicit cast can be used until we implement this kind of translation, assuming we decide to do so. For example: var title = "title";
var queryable = context.Articles.Where(a => a.Title == (ArticleTitle)title); |
The cast will invoke the Would you be open to a PR, if I could somehow figure out how to implement this? (I may not be capable though) |
@glen-84 I will need to discuss with the team. |
@glen-84 We discussed this and came to the conclusion that handling this case requires conversion on one side or the other. From my testing, this works: var title = "title";
var queryable = context.Articles.Where(a => (string)a.Title == title); As does this: var title = "title";
var queryable = context.Articles.Where(a => (object)a.Title == title); This tells EF to treat both sides like a string, without doing any evaluation on the parameter. We don't intend to support anything more than this. |
Thanks a lot for getting back to me on this. Both of those options result in an unnecessary SQL SELECT `e`.`id` AS `Id`, `e`.`title` AS `Title`
FROM `editorial_articles` AS `e`
WHERE CAST(`e`.`title` AS char) = @__title_0 I'm also not sure if tools designed for building EF Core queries would know to add the casts anyway. Having said this, we've decided to move away from using value objects in our web and infrastructure layers, as there are a lot of issues between EF Core and Hot Chocolate (the framework that we're using for our API) when trying to make this all work together. I appreciate your help. |
Ask a question
I have a value object named
ArticleTitle
, that wraps a string. I also have the necessary type converter:A query like this works:
But if you try to compare with the underlying type (string):
It fails with:
I don't see a reason why the underlying type could not be accepted, and passed directly to the database.
EF Core knows the underlying type of the property, and could simply accept that (in addition to the exact type).
This would allow us to perform queries without requiring value objects to be constructed, which could throw exceptions. It would also mean that a web/UI layer would not be forced to work with the value objects in the domain.
Include provider and version information
EF Core version: 7.0.2
Database provider: Pomelo.EntityFrameworkCore.MySql
Target framework: .NET 7.0
Operating system: Windows, WSL
IDE: n/a
The text was updated successfully, but these errors were encountered: