I am attempting to get a functional test C# app to work with Entity Framework against our IBMi DB2 database. To start basic, I chose a small/simple database table to verify all works. I can connect successfully and even can tell that it can "see" the table/data, however, when testing the retrieval of the response object, I am met with an error saying "Specified cast is not valid".
I can tell the query works as the resulting count is correct, but iterating the results always give me a cast error which given it is based on an integer call, I can tell what column it is (TYPNR). I have tried other data types (int16, etc.), but nothing is working. Anyone else run into something like this that they can share their experience?
It feels like a very basic stuck point, but I'm getting close to banging my head, so reaching out for help. Thank you in advance.
Details below of table, object code snippet and error below.
--------------------------------
Table:CREATE TABLE "HSDATA"."TYTYPF" (
"TYRCD" CHAR(2) CCSID 37 NOT NULL,
"TYARC" CHAR(1) CCSID 37 NOT NULL,
"TYPNR" NUMERIC(2 , 0) NOT NULL,
"TYPDS" CHAR(25) CCSID 37 NOT NULL,
"TYSA1" CHAR(5) CCSID 37 NOT NULL,
"TYSN1" DECIMAL(9 , 2) NOT NULL );
Class: class TYTYPF
{
public string TYRCD { get; set; }
public string TYARC { get; set; }
[Key]
public long TYPNR { get; set; }
public string TYPDS { get; set; }
public string TYSA1 { get; set; }
public decimal TYSN1 { get; set; }
}
Code Snippet: var results = context.TYTYPF.Where(x => x.TYPNR == 1 || x.TYPNR == 2);
Console.WriteLine("Count: " + results.Count());
foreach (var record in results) // fails on this line
{
Console.WriteLine(" Type: " + record.TYPNR.ToString() +
"\n Description: " + record.TYPDS);
}
Error: System.InvalidCastException: Specified cast is not valid.
at IBM.Data.DB2.Core.DB2DataWrapper.get_Int32Value()
at IBM.Data.DB2.Core.DB2DataReader.GetInt32(Int32 i)
at lambda_method(Closure , QueryContext , DbDataReader , ResultContext , Int32[] , ResultCoordinator )
at Microsoft.EntityFrameworkCore.Query.Internal.QueryingEnumerable`1.Enumerator.MoveNext()
------------------------------
Shaun
------------------------------
#Db2