我在使用Dapper查询参数时遇到了一些小问题。
我有:
public class QueryFilterItem
{
public string Name { get; }
public string FieldName { get; }
public object Value { get; }
public ComparisonType ComparisonType { get; }
}var arguments = new ExpandoObject();
// ...
arguments.TryAdd($"{filter.Name}Entity{filter.FieldName}", filter.Value);
// ... This part above is a part of another helper class, which is dynamically building filter query data
IEnumerable<dynamic> result;
using (var connection = DbConnectionFactory.CreateDbConnection())
{
result = connection.Query(sqlQuery, arguments); // tried with wrapping into DynamicParameters
}从这个代码中,我得到了一个例外:
System.NotSupportedException: The member MyFilterItemEntityId
of type System.Text.Json.JsonElement cannot be used as a parameter value
at Dapper.SqlMapper.LookupDbType(Type type, String name, Boolean demand, ITypeHandler& handler) in /_/Dapper/SqlMapper.cs:line 418
at Dapper.DynamicParameters.AddParameters(IDbCommand command, Identity identity) in /_/Dapper/DynamicParameters.cs:line 232
at Dapper.DynamicParameters.Dapper.SqlMapper.IDynamicParameters.AddParameters(IDbCommand command, Identity identity) in /_/Dapper/DynamicParameters.cs:line 151
at Dapper.SqlMapper.<>c__DisplayClass165_0.<GetCacheInfo>b__0(IDbCommand cmd, Object obj) in /_/Dapper/SqlMapper.cs:line 1714
at Dapper.CommandDefinition.SetupCommand(IDbConnection cnn, Action`2 paramReader) in /_/Dapper/CommandDefinition.cs:line 129
at Dapper.SqlMapper.<QueryImpl>d__140`1.MoveNext() in /_/Dapper/SqlMapper.cs:line 1080
at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
at Dapper.SqlMapper.Query[T](IDbConnection cnn, String sql, Object param, IDbTransaction transaction, Boolean buffered, Nullable`1 commandTimeout, Nullable`1 commandType) in /_/Dapper/SqlMapper.cs:line 725
at Dapper.SqlMapper.Query(IDbConnection cnn, String sql, Object param, IDbTransaction transaction, Boolean buffered, Nullable`1 commandTimeout, Nullable`1 commandType) in /_/Dapper/SqlMapper.cs:line 652
at DappertStorageTest.Cqrs.SelectQueryHandler.GetByFilters(SelectQuery query)
at DappertStorageTest.Cqrs.SelectQueryHandler.Handle(SelectQuery query)
at System.Dynamic.UpdateDelegates.UpdateAndExecute2[T0,T1,TRet](CallSite site, T0 arg0, T1 arg1)
at MAS.DappertStorageTest.Cqrs.Infrastructure.QueryProcessor.Execute[TResult](IQuery`1 query)
at MAS.DapperStorageTest.Controllers.DataController.Select(SelectRequest selectRequest)
at Microsoft.Extensions.Internal.ObjectMethodExecutor.Execute(Object target, Object[] parameters)
at Microsoft.AspNetCore.Mvc.Infrastructure.ActionMethodExecutor.SyncObjectResultExecutor.Execute(IActionResultTypeMapper mapper, ObjectMethodExecutor executor, Object controller, Object[] arguments)
at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.<<InvokeActionMethodAsync>g__Logged|12_1>d.MoveNext()参数值(不是异常的一部分):
sqlQuery:使用DapperStorageTest;从乘客位置选择* (Id = @MyFilterItemEntityId)
参数:{MyFilterItemEntityId,ValueKind = String:"7d570abd-576d-425f-9d24-d84314d299ba"} (raw)
有人能帮我吗?
发布于 2020-12-28 09:23:56
由于xanatos异常导致了web模型的映射。根据api模型,过滤器值具有object类型,asp.net核心将字符串映射为JsonElement。从object切换到string类型。
public class QueryFilterItem
{
public string Name { get; }
public string FieldName { get; }
public object Value { get; } // Here
public ComparisonType ComparisonType { get; }
}https://stackoverflow.com/questions/65474771
复制相似问题