title: How to use Dapper micro-ORM with Oracle to map NUMBER (OracleDecimal)
body: <p>The ODP.NET provider raises an exception in IDataReader.GetValue()/GetValues() if the column type is NUMBER(x,y) such that it will overflow all .NET numeric types. So Dapper is unable to map such a column to a POCO property.</p>
  
  <p>I have an Oracle stored procedure that uses a REF CURSOR output parameter to return 3-column records. Fundamentally all 3 are NUMBER(something), but the <a href="https://www.nuget.org/packages/odp.net.managed/" rel="nofollow">ODP.NET Oracle managed provider</a> seems to decide what ODP.NET or .NET type to turn them into. </p>
  
  <p>I've been having problems with Dapper's Query() mapping records from this sproc into POCOs. Perhaps it actually isn't my fault, for once - it seems when a column comes across as an ODP.NET type instead of a .NET type, Dapper fails. If I comment an offending column out of my POCO, everything works.</p>
  
  <p>Here's a pair of rows to illustrate:</p>
  
  <pre><code>--------------------------------------------------------------------
  RDWY_LINK_ID           RLC_LINK_OSET          SIGN                   
  ---------------------- ---------------------- ---------------------- 
  1829                   1.51639964279667746989761971196153763602 1 
  14380                  578.483600357203322530102380288038462364 -1 
  </code></pre>
  
  <p>The first column is seen in .NET as <strong>int</strong>, the second column as type <a href="http://docs.oracle.com/cd/B28359_01/win.111/b28375/featTypes.htm" rel="nofollow"><strong>OracleDecimal</strong></a>, and the third as <strong>decimal</strong>. The second one is the problem. </p>
  
  <p>For example, removing Dapper for the moment and using vanilla ODP.NET to access these records thusly indicates the problem:</p>
  
  <pre><code>int linkid = (int)reader.GetValue(0);
  decimal linksign = (decimal)reader.GetValue(2);
  //decimal dlinkoffset = (decimal)reader.GetValue(1); //**invalid cast exception at at Oracle.ManagedDataAccess.Client.OracleDataReader.GetDecimal(Int32 i)**
  //object olinkoffset = reader.GetValue(1); //**same**
  //decimal dlinkoffset = reader.GetDecimal(1); //**same**
  //object[] values = new object[reader.FieldCount];
  //reader.GetValues(values); //**same**
  OracleDecimal linkoffset = (OracleDecimal)reader.GetProviderSpecificValue(1); //this works!
  double dblinkoffset = reader.GetDouble(1); //interesting, this works too!
  //decimal dlinkoffset = linkoffset.Value; //overflow exception
  dblinkoffset = linkoffset.ToDouble(); //voila
  </code></pre>
  
  <p>What little browsing and breakpointing I've done in Dapper's SqlMapper.cs file shows me that it is extracting data from the reader with GetValue()/GetValues(), as above, which fails.</p>
  
  <p>Any suggestions how to patch Dapper up?  Many thanks.</p>
  
  <p>UPDATE:</p>
  
  <p>Upon reflection, I RTFMed: Section 3, "Obtaining Data from an OracleDataReader Object" of the <a href="http://docs.oracle.com/cd/B28359_01/win.111/b28375/toc.htm" rel="nofollow">Oracle Data Provider for .NET Developer’s Guide</a> which explains. For NUMBER columns, ODP.NET's OracleDataReader will try a sequence of .NET types from Byte to Decimal to prevent overflow. But a NUMBER may still overflow Decimal, giving an invalid cast exception if you try any of the reader's .NET type accessors (GetValue()/GetValues()), in which case you have to use the reader's ODP.NET type accessor GetProviderSpecificValue(), which gives you an OracleDecimal, and if it overflows a Decimal, its Value property will give you an overflow exception and your only recourse is to coerce it into a lesser type with one of OracleDecimal's ToXxx() methods.</p>
  
  <p>But of course the ODP.NET type accessor is not part of the IDataReader interface used by Dapper to hold reader objects, so it seems that Dapper, by itself, is Oracle-incompatible when a column type will overflow all .NET types. </p>
  
  <p>The question remains - do the smart folk know how to extend Dapper to handle this. It seems to me I'd need an extension point where I could provide implementation on how to use the reader (forcing it to use GetDouble() instead of GetValue(), or casting to OracleDataReader and calling GetProviderSpecificValue()) for certain POCO property or column types.</p>