gpt4 book ai didi

c# - Linq 'Index was outside the bounds of the array'问题

转载 作者:行者123 更新时间:2023-11-30 16:33:50 34 4
gpt4 key购买 nike

在尝试使用 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所以发帖。看起来它可能与您的 DBML 有关...

关于c# - Linq 'Index was outside the bounds of the array'问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2866246/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com