我有一张桌子,我们叫Users
.这个表有一个在 SQL Server 中定义的主键 - 一个自动增量 int ID
.
I have a table, we'll call Users
. This table has a single primary key defined in SQL Server - an autoincrement int ID
.
有时,我对这个表的 LINQ 查询会失败,并出现 索引超出范围"
错误 - 即使是最简单的查询.查询本身不使用任何索引器.
Sometimes, my LINQ queries against this table fail with an "Index was outside the range"
error - even the most simplest of queries. The query itself doesn't use any indexers.
例如:
User = Users.Take(1);
或
IEnumerable<Users> = Users.ToList();
两个查询都抛出了相同的错误.使用调试器 Visualizer 查看生成的查询 - 我将查询复制并粘贴到 SQL 中,它工作正常.我还在可视化器上单击执行",它工作正常.但是单独执行代码会引发此错误.我没有在类上实现任何部分方法,所以那里什么也没有发生.如果我重新启动调试器,问题就会消失,只是在几个小时后随机再次抬起头.更重要的是,我在生产中运行的应用程序的错误日志中看到了这个错误.
Both of the queries threw the same error. Using the debugger Visualizer to look at the generated query - I copy and paste the query in SQL and it works fine. I also click "execute" on the visualizer and it works fine. But executing the code by itself throws this error. I don't implement any of the partial methods on the class, so nothing is happening there. If I restart my debugger, the problem goes away, only to rear it's head again randomly a few hours later. More critically, I see this bug in my error logs from the app running in production.
我在我的应用程序中针对数据库中的十几个不同实体执行了大量 LINQ,但我只在与表中特定实体相关的查询中看到此问题.一些谷歌搜索表明这个问题可能与我的模型和另一个实体之间指定的不正确关系有关,但我与这个对象没有任何关系.它似乎 95% 的时间都在工作,只有另外 5% 的时间失败了.
I do a ton of LINQ in my app, against a dozen or so different entities in my database, but I only see this problem on queries related to a specific entity in my table. Some googling has suggested that this problem might be related to an incorrect relationship specified between my model and another entity, but I don't have any relationships with this object. It seems to be working 95% of the time, it's just the other 5% that fail.
我已经从设计器中完全删除了对象,并从刷新"的服务器浏览器中重新添加了它,但并没有解决问题.
I have completely deleted the object from the designer, and re-added it from a "refreshed" server browser, and that did not fix the problem.
知道这里发生了什么吗?
Any ideas what's going on here?
这是完整的错误消息和堆栈跟踪:
Here's the full error message and stack trace:
索引超出范围.必须是非负的并且小于集合.参数名称:index atSystem.Data.Linq.SqlClient.SqlProvider.Execute(表达式查询,QueryInfo queryInfo, IObjectReaderFactory 工厂, Object[]parentArgs, Object[] userArgs, ICompiledSubQuery[] subQueries, Object最后结果)在System.Data.Linq.SqlClient.SqlProvider.ExecuteAll(表达式查询,QueryInfo[] queryInfos、IObjectReaderFactory 工厂、Object[]userArguments, ICompiledSubQuery[] subQueries) 在System.Data.Linq.SqlClient.SqlProvider.System.Data.Linq.Provider.IProvider.Execute(表达式查询)在System.Data.Linq.Table1.System.Linq.IQueryProvider.Execute[TResult](表达式表达式)在System.Linq.Queryable.FirstOrDefault[TSource](IQueryable
1 源,表达式`1 谓词)在 MyProject.FindUserByType(String typeId)
Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index at System.Data.Linq.SqlClient.SqlProvider.Execute(Expression query, QueryInfo queryInfo, IObjectReaderFactory factory, Object[] parentArgs, Object[] userArgs, ICompiledSubQuery[] subQueries, Object lastResult) at System.Data.Linq.SqlClient.SqlProvider.ExecuteAll(Expression query, QueryInfo[] queryInfos, IObjectReaderFactory factory, Object[] userArguments, ICompiledSubQuery[] subQueries) at System.Data.Linq.SqlClient.SqlProvider.System.Data.Linq.Provider.IProvider.Execute(Expression query) at System.Data.Linq.Table
1.System.Linq.IQueryProvider.Execute[TResult](Expression expression) at System.Linq.Queryable.FirstOrDefault[TSource](IQueryable
1 source, Expression`1 predicate) at MyProject.FindUserByType(String typeId)
根据要求,下面是表架构的副本.
As requested, below is a copy of the table schema.
CREATE TABLE [dbo].[Container](
[ID] [int] IDENTITY(1,1) NOT NULL,
[MarketCode] [varchar](max) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,
[Description] [varchar](max) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,
[Capacity] [int] NOT NULL,
[Volume] [float] NOT NULL
CONSTRAINT [PK_Container] PRIMARY KEY CLUSTERED
(
[ID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
堆栈跟踪显示 FirstOrDefault
,但我使用 Take()
和 ToList()
复制了错误.所有这些之间的堆栈跟踪是相同的,只需互换 FirstOrDefault/Take/ToList
.将堆栈向下移动到 SqlProvider.Execute
实际上是相同的.
The stack trace shows FirstOrDefault
, but I duplicated the error using both Take()
and ToList()
. The stack trace is identical between all of these, simply interchangnig FirstOrDefault/Take/ToList
. The move down the stack to SqlProvider.Execute
is in fact identical.
这几乎肯定不会是每个人的根本原因,但我在我的项目中遇到了这个完全相同的异常 - 并发现根本原因是一个异常在实体类的构造过程中被抛出.奇怪的是,真正的异常是丢失"的,而是表现为一个 ArgumentOutOfRange 异常,该异常源自检索对象的 Linq 语句的迭代器.
This almost certainly won't be everyone's root cause, but I encountered this exact same exception in my project - and found that the root cause was that an exception was being thrown during construction of an entity class. Oddly, the true exception is "lost" and instead manifests as an ArgumentOutOfRange exception originating at the iterator of the Linq statement that retrieves the object/s.
如果您收到此错误并且在 POCO 上引入了 OnCreated 或 OnLoaded 方法,请尝试逐步执行这些方法.
If you are receiving this error and you have introduced OnCreated or OnLoaded methods on your POCOs, try stepping through those methods.
这篇关于奇怪的 LINQ 异常(索引越界)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!