Hi Team,
I am using .Net Core 2.2 and EF Core 2.2.
I have ColumnX in database with type NUMBER(5).
When I scaffold the table it will generate C# datatype as short.
As Number(5) database can contain value 999999. However C# type short can have max value 32767.
Now when the ColumnX has a value greater than 32767, the application raises an exception the value expected is Int16 but the actual value is Int16.
To resolve this issue changed the datatype of ColumnX from short to int.
However, I don't want my datatype to change to NUMBER(10) on migration, in fluent API, I added below code.
entity.Property<int>(e => e.ColumnX)
.HasColumnName("ColumnX")
.HasColumnType("NUMBER(5)");
When I do that I receive below exception.
System.InvalidOperationException: An exception occurred while reading a database value for property 'Table.ColumnX'. The expected type was 'System.Int32' but the actual value was of type 'System.Int32'. ---> System.InvalidCastException: Specified cast is not valid.
at Oracle.ManagedDataAccess.Client.OracleDataReader.GetInt16(Int32 i)
at lambda_method(Closure , DbDataReader )
--- End of inner exception stack trace ---
at Microsoft.EntityFrameworkCore.Storage.TypedRelationalValueBufferFactoryFactory.ThrowReadValueException[TValue](Exception exception, Object value, IPropertyBase property)
at lambda_method(Closure , DbDataReader )
at Microsoft.EntityFrameworkCore.Storage.Internal.TypedRelationalValueBufferFactory.Create(DbDataReader dataReader)
at Microsoft.EntityFrameworkCore.Query.Internal.QueryingEnumerable`1.Enumerator.BufferlessMoveNext(DbContext _, Boolean buffer)
at Oracle.EntityFrameworkCore.Storage.Internal.OracleExecutionStrategy.Execute[TState,TResult](TState state, Func`3 operation, Func`3 verifySucceeded)
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() System.InvalidOperationException: An exception occurred while reading a database value for property 'Table.ColumnX'. The expected type was 'System.Int32' but the actual value was of type 'System.Int32'. ---> System.InvalidCastException: Specified cast is not valid.
at Oracle.ManagedDataAccess.Client.OracleDataReader.GetInt16(Int32 i)
at lambda_method(Closure , DbDataReader )
--- End of inner exception stack trace ---
at Microsoft.EntityFrameworkCore.Storage.TypedRelationalValueBufferFactoryFactory.ThrowReadValueException[TValue](Exception exception, Object value, IPropertyBase property)
at lambda_method(Closure , DbDataReader )
at Microsoft.EntityFrameworkCore.Storage.Internal.TypedRelationalValueBufferFactory.Create(DbDataReader dataReader)
at Microsoft.EntityFrameworkCore.Query.Internal.QueryingEnumerable`1.Enumerator.BufferlessMoveNext(DbContext _, Boolean buffer)
at Oracle.EntityFrameworkCore.Storage.Internal.OracleExecutionStrategy.Execute[TState,TResult](TState state, Func`3 operation, Func`3 verifySucceeded)
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()
The only way I can make it work is if omit datatype.
entity.Property<int>(e => e.ColumnX)
.HasColumnName("ColumnX");
To me, it seems like an issue.
Let me know your thoughts.