LINQ:IndexOutOfRangeException

时间:2011-05-12 20:02:13

标签: 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: 

2 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:0)

似乎LINQ To SQL(有/有)组合键有问题。实际来源为MultiKeyManagerTryCreateKeyFromValues(Object[] values

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

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

该文章的关键引用:

  

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