实体框架“在不存在数据时进行无效的读取尝试".在Azure上使用``大''数据 [英] Entity Framework "Invalid attempt to read when no data is present" with 'large' data on Azure

查看:89
本文介绍了实体框架“在不存在数据时进行无效的读取尝试".在Azure上使用``大''数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Entity Framework(v4.0)连接到SQL Azure(我安装了March SDK),并在尝试查询表时得到一个InvalidOperationException.异常消息为Invalid attempt to read when no data is present.,并且堆栈跟踪清楚地表明,当它尝试获取列标题时,这在EF内部失败:

I am using Entity Framework (v4.0) to connect to SQL Azure (I have the March SDK installed) and getting a InvalidOperationException when trying to query a table. The message of the exception is Invalid attempt to read when no data is present. and the stack trace clearly shows that this is failing internally in EF when it attempts to get the column header:

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[]& outputs) 
at System.ServiceModel.Dispatcher.DispatchOperationRuntime.InvokeBegin(MessageRpc& rpc)

这与第二张表中的列中的数据特别相关(下例中的设置).如果我查询另一个表(例如下面的示例中的用户")或排除查询该列,则此方法可以很好地工作. 代码示例:

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:

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(); 
        } 
    } 
}   

我尝试重新创建表,更改表名称,列名称和数据类型,但是这些都无济于事.如果表为空或该列包含一组较小"的数据,那么它可以工作,但是当我在其中只有一行包含较大"的数据时,它就会失败!

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!

小"和我"是什么意思大,好吧,它们并不是真正的小巧&.对于SQL来说很大:

What do I mean by small & large, well they not really smal & large for SQL:

  • 'small'< 〜8k
  • '大'>〜8k

我可以确认问题与我无关

I can confirm the issue is not related to me disposing the context early.


更新

  1. 这只是只读的,插入效果很好.
  2. 当我仅使用EF使用LINQ to SQL时,不会发生这种情况.
  3. 错误已登录Microsoft ,因为我怀疑这不是正常行为.
  1. This is only on read, inserts work fine.
  2. This does not occur when I use LINQ to SQL, just with EF.
  3. Bug logged with Microsoft as I suspect this is not normal behaviour.

推荐答案

我增加了命令超时时间,并且有效.

I increased the command timeout, and it worked.

using (var db = new DBEntities()) 
{ 
//setting the CommandTimeout before the .ToList()
db.CommandTimeout = 120;

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(); 
    } 
} 
}   

这篇关于实体框架“在不存在数据时进行无效的读取尝试".在Azure上使用``大''数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆