You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Failure when during array value conversion over nullable element type with non-nullable user conversion lambda #2189
System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation.
---> System.ArgumentException: Expression of type 'Namespace.EnumType' cannot be used for parameter of type 'System.Nullable`1[Namespace.EnumType]' of method 'Void Add(System.Nullable`1[Namespace.EnumType])' (Parameter 'arg0')
at System.Dynamic.Utils.ExpressionUtils.ValidateOneArgument(MethodBase method, ExpressionType nodeKind, Expression arguments, ParameterInfo pi, String methodParamName, String argumentParamName, Int32 index)
at System.Linq.Expressions.Expression.Call(Expression instance, MethodInfo method, Expression arg0)
at System.Linq.Expressions.Expression.Call(Expression instance, MethodInfo method, IEnumerable`1 arguments)
at Npgsql.EntityFrameworkCore.PostgreSQL.Storage.ValueConversion.NpgsqlArrayConverter`2.ArrayConversionExpression[TInput,TOutput](LambdaExpression elementConversionExpression)
at Npgsql.EntityFrameworkCore.PostgreSQL.Storage.ValueConversion.NpgsqlArrayConverter`2.FromArrayProviderExpression(LambdaExpression elementFromProviderExpression)
at Npgsql.EntityFrameworkCore.PostgreSQL.Storage.ValueConversion.NpgsqlArrayConverter`2..ctor(ValueConverter elementConverter)
changed the title [-]System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation.[/-][+]Failure when during array value conversion over nullable element type with non-nullable user conversion lambda[/+]on Dec 30, 2021
Activity
hindams commentedon Dec 28, 2021
After some digging, I think there's an issue here:
efcore.pg/src/EFCore.PG/Storage/ValueConversion/NpgsqlArrayConverter.cs
Line 68 in 40f29d5
We're unwrapping the nullable type while converting the array, but the parameter of the expression seems to remain nullable.
I tried this too but didn't solve it:
var result = this.db.Entities.Where(e => LinkingAttributeTypes.Contains(e.EnumProperty.Value));hindams commentedon Dec 29, 2021
This is the easiest way I found to make it work, basically make sure none of the types are nullable
var result = this.db.Entities.Where(e => LinkingAttributeTypes.Cast<EnumType>().Contains((EnumType )e.EnumProperty.Value));It doesn't make sense to make this change at our codebase to support this scenario.
roji commentedon Dec 29, 2021
Can you please submit a runnable code sample? The code below works fine for me on EF Core 6.0:
Attempted repro
hindams commentedon Dec 29, 2021
I was missing the string to enum conversion, It might be part of the problem here:
hindams commentedon Dec 29, 2021
I tried ValueConverter<EnumType?, string> too, it didn't work but feel to try it, maybe I'm missing something.
Fix value conversion of array over nullable
roji commentedon Dec 30, 2021
Thanks @hindams, I can see the issue. I've submitted #2197 to fix this for 6.0.3.
[-]System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation.[/-][+]Failure when during array value conversion over nullable element type with non-nullable user conversion lambda[/+]Fix value conversion of array over nullable (#2197)
Fix value conversion of array over nullable (#2197)
roji commentedon Dec 30, 2021
Backported to 6.0.3 via 1678cda
emadelwany commentedon Dec 30, 2021
Thanks @roji for an incredibly fast resolution! It's always a pleasure working with Npgsql.
roji commentedon Dec 30, 2021
My pleasure. You can grab the latest daily build from the patch feed to test and confirm this works (I'd be happy to know).
hindams commentedon Dec 30, 2021
Thanks @roji for the fast fix, I tried the patch feed for this specific scenario and it worked, will update you if I found any other related issues.
roji commentedon Dec 31, 2021
Great, thanks for the confirmation!
hindams commentedon Jan 21, 2022
@roji is there any update on 6.0.3 release?