开发者

LINQ: IndexOutOfRangeException

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: 


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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜