Skip to content

Commit a48dc8e

Browse files
committed
[fix] An exception was thrown on conversion to text for numeric fields in FullTextSearchQuery
1 parent c060af2 commit a48dc8e

1 file changed

Lines changed: 11 additions & 2 deletions

File tree

easydata.net/src/EasyData.EntityFrameworkCore.Relational/Services/LinqExtensions.cs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -125,8 +125,17 @@ private static Exp CreateSubExpression(Exp expr, Type type, string text, FullTex
125125
toStringExp = Exp.Call(convertToString, valueExp);
126126
}
127127
else {
128-
var convertToString = typeof(Convert).GetMethod("ToString", new[] { prop.PropertyType });
129-
toStringExp = Exp.Call(convertToString, Exp.Convert(paramExp, prop.PropertyType));
128+
#if NET5_0_OR_GREATER
129+
// EF Core 5+ can translate instance .ToString() on numeric types to SQL (CAST)
130+
var numericType = prop.PropertyType.IsNullable()
131+
? Nullable.GetUnderlyingType(prop.PropertyType)
132+
: prop.PropertyType;
133+
var toStringMethod = numericType.GetMethod("ToString", Type.EmptyTypes);
134+
toStringExp = Exp.Call(Exp.Convert(paramExp, numericType), toStringMethod);
135+
#else
136+
// EF Core 2/3 cannot translate numeric .ToString() to SQL — skip numeric properties
137+
continue;
138+
#endif
130139
}
131140
}
132141

0 commit comments

Comments
 (0)