問題描述
我有一張桌子,我們叫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 異常(索引越界)的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!