@@ -97,7 +97,8 @@ public NpgsqlJsonPocoTranslator(
97
97
/// doing so can result in application failures when updating to a new Entity Framework Core release.
98
98
/// </summary>
99
99
public virtual SqlExpression ? TranslateMemberAccess ( SqlExpression instance , SqlExpression member , Type returnType )
100
- => instance switch
100
+ {
101
+ return instance switch
101
102
{
102
103
// The first time we see a JSON traversal it's on a column - create a JsonTraversalExpression.
103
104
// Traversals on top of that get appended into the same expression.
@@ -119,6 +120,25 @@ PgJsonTraversalExpression prevPathTraversal
119
120
_ => null
120
121
} ;
121
122
123
+ // The PostgreSQL traversal operator always returns text.
124
+ // If the type returned is a scalar (int, bool, etc.), we need to apply a conversion from string.
125
+ SqlExpression ConvertFromText ( SqlExpression expression , Type returnType )
126
+ => _typeMappingSource . FindMapping ( returnType . UnwrapNullableType ( ) , _model ) switch
127
+ {
128
+ // Type mapping not found - this isn't a scalar
129
+ null => expression ,
130
+
131
+ // Arrays are dealt with as JSON arrays, not array scalars
132
+ NpgsqlArrayTypeMapping => expression ,
133
+
134
+ // Text types don't a a conversion to string
135
+ { StoreTypeNameBase : "text" or "varchar" or "char" } => expression ,
136
+
137
+ // For any other type mapping, this is a scalar; apply a conversion to the type from string.
138
+ var mapping => _sqlExpressionFactory . Convert ( expression , returnType , mapping )
139
+ } ;
140
+ }
141
+
122
142
/// <summary>
123
143
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
124
144
/// the same compatibility standards as public APIs. It may be changed or removed without notice in
@@ -159,38 +179,4 @@ PgJsonTraversalExpression prevPathTraversal
159
179
return null ;
160
180
}
161
181
}
162
-
163
- // The PostgreSQL traversal operator always returns text, so we need to convert to int, bool, etc.
164
- private SqlExpression ConvertFromText ( SqlExpression expression , Type returnType )
165
- {
166
- var unwrappedReturnType = returnType . UnwrapNullableType ( ) ;
167
-
168
- switch ( Type . GetTypeCode ( unwrappedReturnType ) )
169
- {
170
- case TypeCode . Boolean :
171
- case TypeCode . Byte :
172
- case TypeCode . DateTime :
173
- case TypeCode . Decimal :
174
- case TypeCode . Double :
175
- case TypeCode . Int16 :
176
- case TypeCode . Int32 :
177
- case TypeCode . Int64 :
178
- case TypeCode . SByte :
179
- case TypeCode . Single :
180
- case TypeCode . UInt16 :
181
- case TypeCode . UInt32 :
182
- case TypeCode . UInt64 :
183
- return _sqlExpressionFactory . Convert ( expression , returnType , _typeMappingSource . FindMapping ( returnType , _model ) ) ;
184
- }
185
-
186
- if ( unwrappedReturnType == typeof ( Guid )
187
- || unwrappedReturnType == typeof ( DateTimeOffset )
188
- || unwrappedReturnType == typeof ( DateOnly )
189
- || unwrappedReturnType == typeof ( TimeOnly ) )
190
- {
191
- return _sqlExpressionFactory . Convert ( expression , returnType , _typeMappingSource . FindMapping ( returnType , _model ) ) ;
192
- }
193
-
194
- return expression ;
195
- }
196
182
}
0 commit comments