其他分享
首页 > 其他分享> > CodeGo.net> Linq的’索引超出了数组的界限’问题

CodeGo.net> Linq的’索引超出了数组的界限’问题

作者:互联网

在尝试设置用于使用C#Linq将项目插入SQL Server Express(2008)数据库的单元测试时,遇到一个错误,这给我带来了一些麻烦. Linq代码是使用Visual Studio 2008构建的.

在运行Windows XP SP2的系统上引发异常. Linq正常工作,并且该记录已正确插入运行Windows 7 Home Premium(64位)的系统上.

我删除并在XP系统上重新创建了数据库.我已经删除并在XP系统上重新创建了DAL和相应的DBML.

其他具有插入同一数据库的单元测试的表也可以正常工作.

使用SQL Server Management Studio中的简单插入语句将记录插入表中是可以正常工作的.

我应该看什么才能找到问题的根源?应该怎么做才能解决问题?

对于错误消息实际上是想说什么的任何见解,将不胜感激.

错误信息

System.IndexOutOfRangeException : Index was outside the bounds of the array.

堆栈跟踪

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 nUnit.DAL.FacilityTests.AddFacility2() in C:\SVN\SVNRevenue360\Branches\Dev\Code\ProviderAdvantage\nUnit.CoreTests\Tests\DAL\FacilityTests.cs:line 50

插入代码

        [Test]
    public void AddFacility2() {
        string facilityname = "Test Facility";
        try {
            using (PA.Database.Revenue360DB db = new PA.Database.Revenue360DB(PA.DAL.DataAccess.ConnectionString)) {
                db.Log = Console.Out;
                PA.Database.Facility facility = new PA.Database.Facility();
                facility.id = Guid.NewGuid();
                facility.Name = facilityname;
                facility.Street1 = "";
                facility.Street2 = "";
                facility.Street3 = "";
                facility.City = "";
                facility.State = "";
                facility.Zip = "";
                facility.Description = "";

                db.Facilities.InsertOnSubmit(facility);
                db.SubmitChanges();  // line 50
            }
        } catch (Exception ex) {
            Console.WriteLine(ex.GetType().FullName + ":  " + ex.Message);
            Console.WriteLine(ex.InnerException == null ? 
                "No inner exception" :
                ex.InnerException.GetType().FullName + ":  " + ex.InnerException.Message);
            throw;
        }
    }

我已经看了以下SO问题,而未深入了解导致此特定问题的原因.

https://stackoverflow.com/questions/1087172/why-am-i-getting-index-was-outside-the-bounds-of-the-array
IndexOutOfRangeException on Queryable.Single
Strange LINQ Exception (Index out of bounds)

解决方法:

看一下this链接.

以下是一些摘录:

A common cause of this error is
associations pointing in the wrong
direction. (Something that is
extremely easy to do if editing the
model by hand, partly because the
association arrow pointer is in the
opposite end of where it would appear
in an ER diagram)

I was having the same problem and it
was due to the fact my primary key was
two columns instead of the traditional
one. (both guids). When I added a
third column that was the sole primary
key column, it worked.

更新:进行了更多探索,发现了this SO帖子.看起来它可能与您的DBML有关…

标签:linq-to-sql,c
来源: https://codeday.me/bug/20191210/2098837.html