title: System.OutOfMemoryException - when Entity Framework is querying a too big data of Varbinary type
body: <p>I'm trying to query a <code>varbinary</code> column that contain a file (1,2 Gb).</p>
  
  <p>I'm using Entity Framework. See below:</p>
  
  <p>Database to test</p>
  
  <pre><code>CREATE TABLE [dbo].[BIGDATA]
  (
      [id] [bigint] IDENTITY(1,1) NOT NULL,
      [BIGDATA] [varbinary](max) NULL, 
  
      CONSTRAINT [PK_BIGDATA] PRIMARY KEY CLUSTERED ([id] ASC) 
  ) ON [PRIMARY]
  </code></pre>
  
  <p>Data to test (any file with 1 Gb)</p>
  
  <pre><code>INSERT INTO [dbo].[BIGDATA]([BIGDATA])
  VALUES
     ((SELECT BulkColumn FROM OPENROWSET(BULK N'C:\BigTest.txt', SINGLE_BLOB) AS Document))
  </code></pre>
  
  <p>Controller to download file</p>
  
  <pre><code>public FileResult Download()
  {
          try
          {
              var context = new Models.ELOGTESTEEntities();
  
              var idArquivo = Convert.ToInt32(1);
  
              // The problem is here, when trying send command to SQL Server to read register
              var arquivo = (from item in context.BIGDATA
                             where item.id.Equals(idArquivo)
                             select item).Single();
              var mimeType = ".txt";              
  
              byte[] bytes = System.Text.Encoding.GetEncoding("iso-8859-8").GetBytes("BigTest.txt");
              return File(arquivo.BIGDATA1, mimeType, System.Text.Encoding.UTF8.GetString(bytes));
          }
          catch (Exception ex)
          {
              throw ex;
          }
      }
  </code></pre>
  
  <p>I can querying normally on SQL Server with <code>Select * From BigData</code>. </p>
  
  <p>But, in Entity Framework (or command with ADO) I get this exception:</p>
  
  <blockquote>
    <p>System.OutOfMemoryException</p>
  </blockquote>
  
  <p>Does someone know how fix this problem?</p>