dotnet / dotnet/efcore

Query: property with converter which handles nulls, can/will give wrong results when compared to null

Open
#29,416 0 comments 0 reactions 0 assignees View on GitHub
area-query
Dominant language
C#
Stars
14.8k
Forks
3.4k
PR merge metrics
PR metrics pending

Description

example scenario: Json_predicate_on_nullableenumwithconverterthathandlesnulls2

property has the following converter:

```cs
b.Property(x => x.TestNullableEnumWithConverterThatHandlesNulls).HasConversion(
new ValueConverter(
x => x == null
? "Null"
: x == JsonEnum.One
? "One"
: x == JsonEnum.Two
? "Two"
: x == JsonEnum.Three
? "Three"
: "INVALID",
x => x == "One"
? JsonEnum.One
: x == "Two"
? JsonEnum.Two
: x == "Three"
? JsonEnum.Three
: null,
convertsNulls: true));
});
```

when the clr value is null, the column stores it as "Null" string. However, when we compare the property to null, we generate the following sql:

```sql

SELECT (...)
FROM [JsonEntitiesAllTypes] AS [j]
WHERE JSON_VALUE([j].[Reference],'$.TestNullableEnumWithConverterThatHandlesNulls') IS NOT NULL
```

we should instead pass the null value thru the converter also so instead we should generate something like this:

```sql
SELECT (...)
FROM [JsonEntitiesAllTypes] AS [j]
WHERE JSON_VALUE([j].[Reference],'$.TestNullableEnumWithConverterThatHandlesNulls') <> 'Null'
```

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.