Dailycode.info

Short solution for short problems

EF: Oracle.ManagedDataAccess Specified Cast is not valid on decimal

Error: Specified cast is not valid. 

CorrId: GetClientAcceptances 

Stack: at Oracle.ManagedDataAccess.Client.OracleDataReader.GetDecimal(Int32 i) 

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

Got this error while working with EF 6 and Oracle DB. Using the Oracle.ManagedDataAccess package. (Version about packages below)


The reason was that in Oracle the number type was set on a column and no precision specified. We had some old data that had some values with a very large precision, and when EF tries to map it to a decimal, there was a specified cast exception. So I rounded all the records in the DB to precision 2, because that was enough for this application and the problem was solved. Some helper query for this:


select RISKFACTOR from MYTABLE where RISKFACTOR - trunc(RISKFACTOR,3) <> 0;
update MYTABLE set RISKFACTOR = ROUND(RISKFACTOR,2) where RISKFACTOR - trunc(RISKFACTOR,3) <> 0;


  <package id="Oracle.ManagedDataAccess" version="12.1.24160719" targetFramework="net461" />

  <package id="Oracle.ManagedDataAccess.EntityFramework" version="12.1.2400" targetFramework="net461" />




blog comments powered by Disqus