.net 实体框架“;当不存在数据时读取的尝试无效”;与';大型';Azure上的数据
我正在使用Entity Framework(v4.0)连接到SQL Azure(我安装了March SDK),并在尝试查询表时获得.net 实体框架“;当不存在数据时读取的尝试无效”;与';大型';Azure上的数据,.net,entity-framework,azure-sql-database,.net,Entity Framework,Azure Sql Database,我正在使用Entity Framework(v4.0)连接到SQL Azure(我安装了March SDK),并在尝试查询表时获得invalidoOperationException。当不存在数据时,异常消息是无效的读取尝试。堆栈跟踪清楚地表明,当尝试获取列标题时,这在EF内部失败: at System.Data.SqlClient.SqlDataReader.ReadColumnHeader(Int32 i) at System.Data.SqlClient.SqlDataReader.Is
invalidoOperationException
。当不存在数据时,异常消息是无效的读取尝试。
堆栈跟踪清楚地表明,当尝试获取列标题时,这在EF内部失败:
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)
这与第二个表中列中的数据(下面示例中的设置)特别相关。如果我查询另一个表(如下面示例中的用户)或排除对该列的查询,这将非常有效。
代码示例:
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();
}
}
}
我尝试过重新创建表,更改表名、列名和数据类型,但没有任何帮助。如果表是空的,或者列包含一组“小”数据,那么它可以工作,但是当我有一行包含“大”数据时,它就失败了
我所说的“小”和“大”是什么意思,它们对于SQL来说并不是真正的“小”和“大”:
- “小”<~8k
- “大”>~8k
更新
增加上下文上的CommandTimeout。我增加了命令超时时间,它起作用了
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();
}
}
}
我也犯了类似的错误。有什么解决办法吗?