简体   繁体   English

异常:指定的转换无效.v

[英]Exception: Specified cast is not valid.v

   at IBM.Data.DB2.Core.TypeMap.FromValue(Object oValue, Int32& iByteLength, DB2Connection connection, Int32 iArrayLength, String parameterName, Boolean bMultiRowCmd)
   at IBM.Data.DB2.Core.DB2Parameter.GetParamInfo(Int32 paramPos, DB2ParamInfo& paramInfo, Boolean chainBegin)
   at IBM.Data.DB2.Core.DB2ParameterCollection.GetParamInfoList(Boolean chainBegin)
   at IBM.Data.DB2.Core.DB2Command.ExecuteReaderObject(CommandBehavior behavior, String method, DB2CursorType reqCursorType, Boolean abortOnOptValueChg, Boolean skipDeleted, Boolean isResultSet, Int32 maxRows, Boolean skipInitialValidation)
   at IBM.Data.DB2.Core.DB2Command.ExecuteReaderObject(CommandBehavior behavior, String method)
   at IBM.Data.DB2.Core.DB2Command.ExecuteReader(CommandBehavior behavior)
   at IBM.Data.DB2.Core.DB2Command.ExecuteDbDataReader(CommandBehavior behavior)
   at System.Data.Common.DbCommand.ExecuteDbDataReaderAsync(CommandBehavior behavior, CancellationToken cancellationToken)
--- End of stack trace from previous location where exception was thrown ---

Exception was thrown at location where it has below lines of code.在代码行以下的位置抛出了异常。 var dr = await cmd.ExecuteReaderAsync() var dr = await cmd.ExecuteReaderAsync()

we have no issue with do.net core 2.1 but when we updated to do.net core 3.1 we are running into this issue.我们对 do.net core 2.1 没有问题,但是当我们更新到 do.net core 3.1 时,我们遇到了这个问题。

update do.netcore 2.1 project used ibm.data.db2.core 1.3 do.netcore 3.1 project using ibm.data.db2.core 3.1(Unit test is passing though)更新do.netcore 2.1 项目使用 ibm.data.db2.core 1.3 do.netcore 3.1 项目使用 ibm.data.db2.core 3.1(虽然单元测试通过)

I got it working now.我现在开始工作了。 In dotnet core 2.1 the object value deserializes properly.在 dotnet core 2.1 中,对象值正确反序列化。 In my case it is string where as in dotnet core 3.1 it is deserializes as JsonElement which was causing the issue and db2 was not recognizing the jsonelement as any type.在我的情况下,它是字符串,在 dotnet core 3.1 中,它被反序列化为 JsonElement,这导致了问题,而 db2 没有将 jsonelement 识别为任何类型。

I switched my service to use Newtonsoftjson by adding services.AddControllers().AddNewtonsoftJson();我通过添加services.AddControllers().AddNewtonsoftJson();将我的服务切换为使用 Newtonsoftjson services.AddControllers().AddNewtonsoftJson(); and now it is properly desearilizing it.现在它正在适当地使它脱盐。

There is an open issue here for bool which is pretty much the same as my case but my object is of type string.这里是一个开放的问题,在这里为布尔这几乎是一样的我的情况,但我的目标是字符串类型。 They also suggest there to write a custom converter for Object.他们还建议在那里为 Object 编写一个自定义转换器。

In case someone stumbles upon this issue:如果有人偶然发现这个问题:

While one should configure desiralization properly, as pointed out by the accepted answer, in my case, I was trying to convert CHARACTER(1) SQL Type to char data type which is the same as Int32 .正如接受的答案所指出的那样,虽然应该正确配置 desiralization,但就我而言,我试图将CHARACTER(1) SQL 类型转换为与Int32相同的char数据类型。

The solution was to change the char data type to string .解决方案是将char数据类型更改为string Inefficient but it does the job.效率低下,但它完成了工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM