Description
openedon Feb 2, 2023
Ask a question
I have a value object named ArticleTitle
, that wraps a string. I also have the necessary type converter:
.HasConversion(
x => x.Value, // string
x => ArticleTitle.From(x));
A query like this works:
dbContext.Articles.Where(a => a.Title == ArticleTitle.From(title))
But if you try to compare with the underlying type (string):
dbContext.Articles.Where(a => a.Title == title)
It fails with:
System.InvalidCastException: Invalid cast from 'System.String' to 'Articles.ArticleTitle'.
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