Contains fails translation when the array is a scalar sbuquery
See original GitHub issueHi there.
I’m using Npgsql.EntityFrameworkCore.PostgreSQL v. 5.0.2
I have a property on an entity which is a list of UserIds (guids) that have read a specific message.
public string[] ReadByParticipantIds { get; set; }
In an OrderByDescending() call I need to find out whether it contains the specific user calling (to get unread messages at the top).
According to this https://www.npgsql.org/efcore/mapping/array.html#operation-translation I should be able to do this. However, I’ve tried just about anything on that list and I keep getting errors.
.ReadByParticipantIds.Contains(userId)
and
.ReadByParticipantIds.Any(s => s == userId)
Both returns the following error:
Npgsql.PostgresException (0x80004005): 42883: operator does not exist: text = text[]
I’ve even tried dropping the equality and using .Any() with the various LIKE-translated expressions. That just changed the error to text ~~~ text[] instead.
It seems like the mapper tries to create an expression where we do equality on a string and an array of strings. I.e. on the root of the .Contains() or .Any() expression rather on the content.
Am I doing something wrong or is this a plain bug?
I do have a modelbuilder expression on this field, but that should only be activated when the DB is not PostGres. So I doubt that could be the culprit:
modelBuilder.Entity<Message>(b =>
{
if (!Database.IsNpgsql())
{
b.Property(p => p.ReadByParticipantIds)
.HasConversion(
v => string.Join("'", v),
v => v.Split(',', StringSplitOptions.RemoveEmptyEntries));
}
});
The reason for the above is that I use an in-memory DB for running unit tests.
- Dan
Issue Analytics
- State:
- Created 2 years ago
- Comments:13 (7 by maintainers)
@roji It seems to work perfectly 😃 I can remove my ugly hack.
Great!