【发布时间】:2011-04-13 22:01:15
【问题描述】:
在尝试设置单元测试以使用 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)
【问题讨论】:
-
您能否指出代码中第 50 行的位置?
-
@Abe 完成。这是 db.SubmitChanges() 调用。
标签: c# linq-to-sql