Db2

 View Only
  • 1.  Data Provider for .NET Core Using Entity Framework

    Posted Wed July 29, 2020 12:10 PM
    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


  • 2.  RE: Data Provider for .NET Core Using Entity Framework

    Posted Wed July 29, 2020 02:00 PM
    I actually figured this one out on my own and it was a simple fix once I realized the issue.  This article helped me get to the epiphany:

    https://community.ibm.com/community/user/hybriddatamanagement/blogs/michelle-betbadal1/2020/04/29/creating-entity-data-model-using-ibm-data-server-p?CommunityKey=f2e5dc34-896d-4e8e-9678-724907c4b9f5&tab=recentcommunityblogsdashboard

    Using the Scaffold-DbContext command allowed me to see the self generated class stubs which clearly pointed out a "Decimal(2,0)" couldn't be directly converted to an integer type. My bad and it seems so obvious now.

    I wanted to post in case it helped anyone down the road.

    ------------------------------
    Shaun Steckley
    ------------------------------



  • 3.  RE: Data Provider for .NET Core Using Entity Framework

    Posted Thu September 03, 2020 05:56 AM
    I Have similar problem with decimal :(

    https://community.ibm.com/community/user/hybriddatamanagement/communities/community-home/digestviewer/viewthread?MessageKey=5bd89058-ed71-4f70-8ebd-072c1312ca6c&CommunityKey=f2e5dc34-896d-4e8e-9678-724907c4b9f5&tab=digestviewer#bm5bd89058-ed71-4f70-8ebd-072c1312ca6c
    do you find resolution ?!

    ------------------------------
    iliyan iliev
    developer MoI-DCIS
    ------------------------------