Oracle.EntityFrameworkCore beta3 Specified cast is not valid — oracle-tech

    Forum Stats

  • 3,715,657 Users
  • 2,242,821 Discussions
  • 7,845,481 Comments

Discussions

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Oracle.EntityFrameworkCore beta3 Specified cast is not valid

Sergey Pechenik
Sergey Pechenik Member Posts: 2
edited April 2019 in ODP.NET

Hello!

I'm trying to read entity from table. But catching

System.InvalidCastException:  'Specified cast is not valid.'

   at Oracle.ManagedDataAccess.Client.OracleDataReader.GetString(Int32 i)

   at lambda_method(Closure , DbDataReader )

   at Microsoft.EntityFrameworkCore.Storage.Internal.TypedRelationalValueBufferFactory.Create(DbDataReader dataReader)

   at Microsoft.EntityFrameworkCore.Query.Internal.QueryingEnumerable`1.Enumerator.BufferAll()

   at Microsoft.EntityFrameworkCore.Query.Internal.QueryingEnumerable`1.Enumerator.BufferlessMoveNext(DbContext _, Boolean buffer)

   at Microsoft.EntityFrameworkCore.Storage.ExecutionStrategy.ExecuteImplementation[TState,TResult](Func`3 operation, Func`3 verifySucceeded, TState state)

   at Microsoft.EntityFrameworkCore.Query.Internal.QueryingEnumerable`1.Enumerator.MoveNext()

   at Microsoft.EntityFrameworkCore.Query.Internal.LinqOperatorProvider._TrackEntities[TOut,TIn](IEnumerable`1 results, QueryContext queryContext, IList`1 entityTrackingInfos, IList`1 entityAccessors)+MoveNext()

   at Microsoft.EntityFrameworkCore.Query.Internal.LinqOperatorProvider.ExceptionInterceptor`1.EnumeratorExceptionInterceptor.MoveNext()

   at System.Collections.Generic.List`1.AddEnumerable(IEnumerable`1 enumerable)

   at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)

   at Opn.Lib.Dotnet.EntityFramework.Core.TestConsole.MyHostedService..ctor(TestDbContext context) in C:\Sources\UFS.Dotnet.Lib\opn.lib.dotnet.entityframework.core\Opn.Lib.Dotnet.EntityFramework.Core.TestConsole\MyHostedService.cs:line 13

Table script:

  CREATE TABLE "ITEMSSTORE"     (     "ID" NUMBER(38,0) NOT NULL ENABLE,       "BIGNUM38" NUMBER(38,0),       "BIGINTCOL" NUMBER(19,0),       "INTCOL" NUMBER(10,0),       "GUIDCOL" RAW(16),       "DATECOL" DATE,       "DATETIMEOFFSETCOL" TIMESTAMP (6) WITH TIME ZONE,       "DECIMALCOL" NUMBER(28,0),       "STRINGCOL" NVARCHAR2(2000),       "DATETIMECOL" TIMESTAMP (6),       "TIMECOL" INTERVAL DAY (2) TO SECOND (6),        CONSTRAINT "ITEMSSTORE_PK" PRIMARY KEY ("ID")    );  INSERT INTO itemsstore (     bignum38,     bigintcol,     intcol,     guidcol,     datecol,     datetimeoffsetcol,     decimalcol,     stringcol,     datetimecol,     timecol ) VALUES (     12345678912345678912345678912345678912,     9223372036854775807,     2147483647,     'fd55e62a0646449f9375a0d86449f77c',     '01-01-2001',     '01-01-01 12:12 +3:00',     9228162514264337593543950335,     N'У попа была собака он её любил, она съела кусок мяса он её убил, закопал её в могилу на могиле написал, у попа была собака он её любил...',     '01-01-01 12:12',     INTERVAL '11:20' HOUR TO MINUTE );

Entity:

    [Table("ITEMSSTORE")]     internal class TestItems     {         [Column("ID")]         [Key]         public long? Id { get; set; }          [Column("BIGNUM38")]         [MaxLength(38)]         public string GiantCol { get; set; }          [Column("BIGINTCOL")]         public long? BigIntCol { get; set; }          [Column("INTCOL")]         public int? IntCol { get; set; }          [Column("GUIDCOL")]         public Guid? GuidCol { get; set; } // В базе это Raw[16]          [Column("DATECOL")]         public DateTime? DateCol { get; set; }          [Column("DATETIMEOFFSETCOL")]         public DateTimeOffset? DateTimeOffsetCol { get; set; }          [Column("DECIMALCOL")]         public decimal? DecCol { get; set; }          [Column("STRINGCOL")]         [MaxLength(2000)]
        public string StrCol { get; set; }          [Column("DATETIMECOL")]         public DateTime? DateTimeCol { get; set; }          [Column("TIMECOL")]         public TimeSpan? TimeCol { get; set; }     }
Sergey Pechenik

Best Answer

  • Alex Keh-Oracle
    Alex Keh-Oracle Posts: 2,751 Employee
    edited April 2019 Accepted Answer

    ODP.NET EF Core provides a default data type mapping between Oracle DB data types and .NET Core data types. Nonetheless, Oracle and .NET data types do not match each others' min and max possible values. You will have to adjust these mappings based on your application data using Annotations or Fluent API.

    The PDF doc contained in the Beta 3 has suggested mappings that you can use based on different size number, text, and binary data.

    Sergey Pechenik

Answers

  • Alex Keh-Oracle
    Alex Keh-Oracle Posts: 2,751 Employee
    edited April 2019 Accepted Answer

    ODP.NET EF Core provides a default data type mapping between Oracle DB data types and .NET Core data types. Nonetheless, Oracle and .NET data types do not match each others' min and max possible values. You will have to adjust these mappings based on your application data using Annotations or Fluent API.

    The PDF doc contained in the Beta 3 has suggested mappings that you can use based on different size number, text, and binary data.

    Sergey Pechenik
  • Sergey Pechenik
    Sergey Pechenik Member Posts: 2
    edited April 2019

    thx, I catch error. NUMBER(38) can't be mapped to string. There is any way to get numbers greater then decimal.MAX?

  • Alex Keh-Oracle
    Alex Keh-Oracle Posts: 2,751 Employee
    edited April 2019

    Well, you can't force a .NET data type to hold more than its maximum value. You can only use another data type that has a larger maximum or alternative storage method that preserves the data.

    You can try manually modifying the mapping after it is generated to a string or another data type that can hold a NUMBER(38).

Sign In or Register to comment.