LINQ:索引超出范围异常
我有一个像这样的 LINQ 实体:
ID1 INT
ID2 INT
ID3 INT
Value VARCHAR(50)
ID1、ID2 & ID3是复合主键。它与任何其他实体没有任何关联。
每当我在 SubmitChanges 之后更新或删除时,都会引发 IndexOutOfRangeException。我在 .NET 3.5 SP1 上运行。
我尝试创建另一个项目,仍在 .NET 3.5 SP1 上,但没有遇到该异常。
这是代码:
DataClasses context = new DataClasses();
Entity entity = (from entities in context.Entities
where entities.ID1 == 3
&& entities.ID2 == 1
&& entities.ID3 == 1
select entities).First();
context.Entities.DeleteOnSubmit(entity);
context.SubmitChanges();
这是例外:
System.IndexOutOfRangeException was unhandled by user code
Message="Index was outside the bounds of the array."
Source="System.Data.Linq"
StackTrace:
at System.Data.Linq.IdentityManager.StandardIdentityManager.MultiKeyManager`3.TryCreateKeyFromValues(Object[] values, MultiKey`2& k)
at System.Data.Linq.IdentityManager.StandardIdentityManager.IdentityCache`2.Find(Object[] keyValues)
at System.Data.Linq.IdentityManager.StandardIdentityManager.Find(MetaType type, Object[] keyValues)
at System.Data.Linq.CommonDataServices.GetCachedObject(MetaType type, Object[] keyValues)
at System.Data.Linq.ChangeProcessor.GetOtherItem(MetaAssociation assoc, Object instance)
at System.Data.Linq.ChangeProcessor.BuildEdgeMaps()
at System.Data.Linq.ChangeProcessor.SubmitChanges(ConflictMode failureMode)
at System.Data.Linq.DataContext.SubmitChanges(ConflictMode failureMode)
at System.Data.Linq.DataContext.SubmitChanges()
at Project.Page.Page_Load(Object sender, EventArgs e) in C:\ProjectFolder\Project\Page.aspx.cs:line 31
at System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp, Object o, Object t, EventArgs e)
at System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e)
at System.Web.UI.Control.OnLoad(EventArgs e)
at System.Web.UI.Control.LoadRecursive()
at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
InnerException:
I have an LINQ entity like so:
ID1 INT
ID2 INT
ID3 INT
Value VARCHAR(50)
ID1, ID2 & ID3 are composite primary keys. It has no association with any other entity.
An IndexOutOfRangeException is thrown whenever I update or delete after SubmitChanges. I'm running on .NET 3.5 SP1.
I have tried creating another project, still on .NET 3.5 SP1, but don't encounter that exception.
Here is the code:
DataClasses context = new DataClasses();
Entity entity = (from entities in context.Entities
where entities.ID1 == 3
&& entities.ID2 == 1
&& entities.ID3 == 1
select entities).First();
context.Entities.DeleteOnSubmit(entity);
context.SubmitChanges();
Here is the exception:
System.IndexOutOfRangeException was unhandled by user code
Message="Index was outside the bounds of the array."
Source="System.Data.Linq"
StackTrace:
at System.Data.Linq.IdentityManager.StandardIdentityManager.MultiKeyManager`3.TryCreateKeyFromValues(Object[] values, MultiKey`2& k)
at System.Data.Linq.IdentityManager.StandardIdentityManager.IdentityCache`2.Find(Object[] keyValues)
at System.Data.Linq.IdentityManager.StandardIdentityManager.Find(MetaType type, Object[] keyValues)
at System.Data.Linq.CommonDataServices.GetCachedObject(MetaType type, Object[] keyValues)
at System.Data.Linq.ChangeProcessor.GetOtherItem(MetaAssociation assoc, Object instance)
at System.Data.Linq.ChangeProcessor.BuildEdgeMaps()
at System.Data.Linq.ChangeProcessor.SubmitChanges(ConflictMode failureMode)
at System.Data.Linq.DataContext.SubmitChanges(ConflictMode failureMode)
at System.Data.Linq.DataContext.SubmitChanges()
at Project.Page.Page_Load(Object sender, EventArgs e) in C:\ProjectFolder\Project\Page.aspx.cs:line 31
at System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp, Object o, Object t, EventArgs e)
at System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e)
at System.Web.UI.Control.OnLoad(EventArgs e)
at System.Web.UI.Control.LoadRecursive()
at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
InnerException:
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我从头开始重新创建了 DBML。叹...
I re-created the DBML from scratch. Sigh...
LINQ To SQL 似乎在组合键方面存在问题。实际来源是
MultiKeyManager
和TryCreateKeyFromValues(Object[]values
这个 ElegantCode post on SubmitChanges throws IndexOutOfRangeException 与你的错误完全相同,条件相似。不同之处在于他有另一个字段来修改 .dbml 模型并标记为 PK。
您是否能够在 .dbml 模型中修改/删除 3 个整数的
PrimaryKey=true
属性?
It appears that LINQ To SQL (has/had) trouble with composite keys. The actual source is
MultiKeyManager
andTryCreateKeyFromValues(Object[] values
This ElegantCode post on SubmitChanges throws IndexOutOfRangeException has the exact same error as yours, with similar conditions. The difference is that he's got another field to modify the .dbml model and mark as PK.
Are you able to modify/remove in your .dbml model the
PrimaryKey=true
attributes for the 3 ints?The key quote from that article: