title: EF + ODP.NET + CLOB = Value Cannot be Null - Parameter name: byteArray?
body: <p>Our project recently updated to the newer Oracle.ManagedDataAccess DLL's (v 4.121.2.0) and this error has been cropping up intermittently.  We've fixed it a few times without really knowing what we did to fix it.</p>
  
  <p>I'm fairly certain it's caused by CLOB fields being mapped to strings in Entity Framework, and then being selected in LINQ statements that pull entire entities instead of just a limited set of properties.</p>
  
  <p>Error:</p>
  
  <pre><code>Value cannot be null.
  Parameter name: byteArray
  </code></pre>
  
  <p>Stack Trace:</p>
  
  <pre><code>   at System.BitConverter.ToString(Byte[] value, Int32 startIndex, Int32 length)
     at OracleInternal.TTC.TTCLob.GetLobIdString(Byte[] lobLocator)
     at OracleInternal.ServiceObjects.OracleDataReaderImpl.CollectTempLOBsToBeFreed(Int32 rowNumber)
     at Oracle.ManagedDataAccess.Client.OracleDataReader.ProcessAnyTempLOBs(Int32 rowNumber)
     at Oracle.ManagedDataAccess.Client.OracleDataReader.Read()
     at System.Data.Entity.Core.Common.Internal.Materialization.Shaper`1.StoreRead()
  </code></pre>
  
  <p>Suspect Entity Properties:</p>
  
  <pre><code>'Mapped to Oracle CLOB Column'
  &lt;Column("LARGEFIELD")&gt;
  Public Property LargeField As String
  </code></pre>
  
  <p>But I'm confident this is the proper way to map the fields per Oracle's matrix:</p>
  
  <p><a href="http://docs.oracle.com/cd/B19306_01/win.102/b14307/featTypes.htm" rel="noreferrer">ODP.NET Types Overview</a></p>
  
  <p>There is nothing obviously wrong with the generated SQL statement either:</p>
  
  <pre><code>SELECT 
  ...
  "Extent1"."LARGEFIELD" AS "LARGEFIELD",
  ...
  FROM ... "Extent1"
  WHERE ...
  </code></pre>
  
  <p>I have also tried this Fluent code per Ozkan's suggestion, but it does not seem to affect my case.</p>
  
  <pre><code>modelBuilder.Entity(Of [CLASS])().Property(
      Function(x) x.LargeField
  ).IsOptional()
  </code></pre>
  
  <p><strong>Troubleshooting Update:</strong></p>
  
  <p>After extensive testing, we are quite certain this is actually a bug, not a configuration problem.  It appears to be the contents of the CLOB that cause the problem under a very specific set of circumstances.  I've cross-posted this on the <a href="https://community.oracle.com/thread/3676588" rel="noreferrer"><strong>Oracle Forums</strong></a>, hoping for more information.</p>