I'm trying to get data from oracle database by using Oracle.ManagedDataAccess in C# Entity Framework. But, when the data type is number in oracle, and the value is 6.17880949622285E-11, i got the error. Here is the error message
{
"ClassName": "System.InvalidCastException",
"Message": "Specified cast is not valid.",
"Data": null,
"InnerException": null,
"HelpURL": null,
"StackTraceString": " at Oracle.ManagedDataAccess.Client.OracleDataReader.GetDecimal(Int32 i)\r\n at Oracle.ManagedDataAccess.Client.OracleDataReader.GetValue(Int32 i)\r\n at System.Data.Entity.Core.Common.Internal.Materialization.Shaper.ErrorHandlingValueReader`1.GetValue(DbDataReader reader, Int32 ordinal)\r\n at System.Data.Entity.Core.Common.Internal.Materialization.Shaper.GetPropertyValueWithErrorHandling[TProperty](Int32 ordinal, String propertyName, String typeName)\r\n at lambda_method(Closure , Shaper )\r\n at System.Data.Entity.Core.Common.Internal.Materialization.Shaper.HandleEntityAppendOnly[TEntity](Func`2 constructEntityDelegate, EntityKey entityKey, EntitySet entitySet)\r\n at lambda_method(Closure , Shaper )\r\n at System.Data.Entity.Core.Common.Internal.Materialization.Coordinator`1.ReadNextElement(Shaper shaper)\r\n at System.Data.Entity.Core.Common.Internal.Materialization.Shaper`1.SimpleEnumerator.MoveNext()\r\n at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)\r\n at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)\r\n at WebApiService.Master.Facul.FaculService.FetchAllProportionalByParameters(FaculModel faculSearch) in D:\\Projects\\Reins\\WebApiService\\Master\\Facul\\FaculService.cs:line 176\r\n at PKBL.Controllers.Master.FaculController.FetchAllProportionalByParameters(FaculModel faculSearch) in D:\\Projects\\Reins\\REINS\\Controllers\\Master\\FaculController.cs:line 74",
"RemoteStackTraceString": null,
"RemoteStackIndex": 0,
"ExceptionMethod": "8\nGetDecimal\nOracle.ManagedDataAccess, Version=4.121.2.0, Culture=neutral, PublicKeyToken=89b483f429c47342\nOracle.ManagedDataAccess.Client.OracleDataReader\nSystem.Decimal GetDecimal(Int32)",
"HResult": -2147467262,
"Source": "Oracle.ManagedDataAccess",
"WatsonBuckets": null
}
i have investigated it, and i found that the problem is the data type. Btw, i'm using code first, here is my EF class
public class MasterFacul : BaseEntityModel
{
....
public decimal? FacWrtShr { get; set; } <- **The Problem**
i have tried to add the precision, but it didn't work. can anyone help me?
The decimal type has a smaller range than the number you are attempting to set it to, hence the error. The Decimal types value is its precision, MS Reference articles state that its better used for financial calculations.
https://learn.microsoft.com/en-us/dotnet/articles/csharp/language-reference/keywords/decimal
For your example data i'd be inclined to use a double, its range is substantial but less precise than the decimal type
https://learn.microsoft.com/en-us/dotnet/articles/csharp/language-reference/keywords/double