Global Data Management Forum

  • 1.  Specified cast is not valid.

    Posted Thu September 03, 2020 04:59 AM
    I Scaffold-DbContext table from db and when i start console app give me exception
    System.InvalidCastException
    HResult=0x80004002
    Message=Specified cast is not valid.
    Source=IBM.Data.DB2.Core
    StackTrace:
    at IBM.Data.DB2.Core.DB2DataWrapper.get_DB2DecimalValue()
    at IBM.Data.DB2.Core.DB2DataReader.GetDecimal(Int32 i)
    at Microsoft.EntityFrameworkCore.Query.Internal.QueryingEnumerable`1.Enumerator.MoveNext()
    at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
    at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)

    script for table :

    CREATE TABLE "admbds"."mailing_bds"
    (
    sid_f decimal(13,255) PRIMARY KEY NOT NULL,
    vid_dok integer NOT NULL,
    email varchar(200) NOT NULL,
    dat_izd date NOT NULL,
    status integer,
    dat_send date,
    cas_send datetime hour to second
    )
    ;
    CREATE UNIQUE INDEX pki_email ON "admbds"."mailing_bds"(sid_f)
    ;

    CREATE INDEX i1_email ON "admbds"."mailing_bds"(dat_send)
    ;

    c# generated class


    public partial class MailingBds
    {
    public TimeSpan? CasSend { get; set; }
    public DateTime DatIzd { get; set; }
    public DateTime? DatSend { get; set; }
    public string Email { get; set; }
    public decimal SidF { get; set; }
    public int? Status { get; set; }
    public int VidDok { get; set; }
    }

    why ?!

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

    #DataManagementGlobal
    #DataServerDrivers


  • 2.  RE: Specified cast is not valid.

    Posted Thu September 03, 2020 09:27 AM
    Iliyan,
    This is just a guess, but the DB table defined as (13, 255) precision is outside the range of the decimal data type in C# which I believe only allows up to 28 significant digits. I would assume that is the reason. I can't really help on resolution though, assuming that is the issue and the table definition can't be changed to reduce that size.

    I was previously able (different situation with date being stored as decimal) to convert data in Entity Framework using OnModelCreating to use the column as a read only field, but you can't filter, sort, etc on the column as the conversion can't be interpreted to SQL correctly.

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



  • 3.  RE: Specified cast is not valid.

    Posted Thu September 03, 2020 09:55 AM
    Edited by System Fri January 20, 2023 04:11 PM
    I don't think that's the problem.
    When use netcore v2.2(3.1) & ibm.db.core v2.2 & ms.entityframeworkcore v2.2 is worked :(

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