LINQ:索引超出范围异常

发布于 2024-11-06 11:18:22 字数 2348 浏览 1 评论 0原文

我有一个像这样的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

苦笑流年记忆 2024-11-13 11:18:22

我从头开始重新创建了 DBML。叹...

I re-created the DBML from scratch. Sigh...

凉月流沐 2024-11-13 11:18:22

LINQ To SQL 似乎在组合键方面存在问题。实际来源是 MultiKeyManagerTryCreateKeyFromValues(Object[]values

这个 ElegantCode post on SubmitChanges throws IndexOutOfRangeException 与你的错误完全相同,条件相似。不同之处在于他有另一个字段来修改 .dbml 模型并标记为 PK。

您是否能够在 .dbml 模型中修改/删除 3 个整数的 PrimaryKey=true 属性

从 SomeType 和 SomeValue 中删除 PrimaryKey=true 属性,并将其添加到 ViewID 属性上,现在 LinqToSql 可以正确处理关联。

It appears that LINQ To SQL (has/had) trouble with composite keys. The actual source is MultiKeyManager and TryCreateKeyFromValues(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:

Remove the PrimaryKey=true attributes from SomeType and SomeValue, and add that on the ViewID property, and now LinqToSql handles the association correctly.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文