title: Entity Framework "Invalid attempt to read when no data is present" with 'large' data on Azure
body: <p>I am using Entity Framework (v4.0) to connect to SQL Azure (I have the March SDK installed) and getting a <code>InvalidOperationException</code> when trying to query a table. The message of the exception is <code>Invalid attempt to read when no data is present.</code> and the stack trace clearly shows that this is failing internally in EF when it attempts to get the column header:   </p>
  
  <pre><code>at System.Data.SqlClient.SqlDataReader.ReadColumnHeader(Int32 i) 
  at System.Data.SqlClient.SqlDataReader.IsDBNull(Int32 i) 
  at lambda_method(Closure , Shaper ) 
  at System.Data.Common.Internal.Materialization.Coordinator`1.ReadNextElement(Shaper shaper) 
  at System.Data.Common.Internal.Materialization.Shaper`1.SimpleEnumerator.MoveNext() 
  at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection) 
  at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source) 
  at Service.LoadSettings() in C:\Service.svc.cs 
  at SyncInvokeLoadSettings(Object , Object[] , Object[] ) 
  at System.ServiceModel.Dispatcher.SyncMethodInvoker.Invoke(Object instance, Object[] inputs, Object[]&amp; outputs) 
  at System.ServiceModel.Dispatcher.DispatchOperationRuntime.InvokeBegin(MessageRpc&amp; rpc)
  </code></pre>
  
  <p>This is specifically related to the data in a column in the second table (Settings in the example below). This works perfectly fine if I query another table (like the Users in the example below) or exclude quering the that column.
  Example of the code:</p>
  
  <pre><code>using (var db = new DBEntities()) 
  { 
      var users = (from u in db.Users 
                   where u.PK == userid 
                   select u).ToList();
  
      if (users.Any()) 
      { 
          var selectedUser = users.Single(); 
          if (selectedUser.Password.Equals(passwordHash)) 
          { 
              // ****************************** 
              // * error is on the next line! * 
              // ****************************** 
              var settings = (from s in db.Settings 
                              where s.User == selectedUser.PK 
                              select s).ToList(); 
          } 
      } 
  }   
  </code></pre>
  
  <p>I've tried re-creating the tables, changing tables names, column names and data types and none of it helps. If the table is empty or the column contains a 'small' set of data then it works but the moment I have a single row in it with a 'large' data it fails!</p>
  
  <p>What do I mean by small &amp; large, well they not really smal &amp; large for SQL:</p>
  
  <ul>
  <li>'small' &lt; ~8k</li>
  <li>'large' > ~8k</li>
  </ul>
  
  <p>I can confirm the issue is not related to me <a href="https://stackoverflow.com/questions/1959592/disposing-datacontext-causes-invalid-attempt-to-call-read-when-reader-is-closed">disposing the context</a> early. </p>
  
  <p><hr/>
  <strong>Updates</strong></p>
  
  <ol>
  <li>This is only on read, inserts work fine.</li>
  <li>This does not occur when I use LINQ to SQL, just with EF.</li>
  <li><a href="https://connect.microsoft.com/VisualStudio/feedback/details/651552/entity-framework-invalid-attempt-to-read-when-no-data-is-present-with-large-data-on-azure" rel="nofollow noreferrer">Bug logged with Microsoft</a> as I suspect this is not normal behaviour.</li>
  </ol>