【发布时间】:2015-07-24 14:00:30
【问题描述】:
我有一些代码在运行时会引发“EntityCommandExecutionException”类型的异常。
Visual Studio 指向的线:
else if (item.FirstOrDefault().InspectionEquipmentTypes.Any())
异常的内部细节说:
There is already an open DataReader associated with this Command which must be closed first.
我的问题是引发错误的那一行没有尝试使用数据库/数据读取器(据我所知),所以我不确定为什么会生成此异常。
编辑:
public static IEnumerable<IGrouping<string,Entities.Inspection>> GetUnscheduledBatchInspections(Entities.EntityModel context)
{
var results = context.Inspections.Where(w =>
w.InspectionBatchNo != null
&& w.IsCancelled == false
&& !w.CalendarItems.Any()
&& w.Duration.HasValue).GroupBy(g => g.InspectionBatchNo);
return results;
}
调用方法:
private void MapBatchInspectionsToViewModel(ref SchedulerViewModel viewModel)
{
var batchInspections = SchedulerManager.GetUnscheduledBatchInspections(this.Context);
foreach (var item in batchInspections)
{
var bigi = new BatchInspectionGridItem();
if (item.Any())
{
bigi.BatchInspectionNo = item.First().InspectionBatchNo;
if (item.FirstOrDefault().EquipmentTypeID != null)
{
bigi.EquipmentTypeName = item.FirstOrDefault().EquipmentType.Description;
}
else if (item.FirstOrDefault().InspectionEquipmentTypes.Any())
{
bigi.EquipmentTypeName = string.Join(" / ", item.FirstOrDefault().InspectionEquipmentTypes.Select(s => s.EquipmentType.Description));
}
bigi.CustomerName = item.First().CustomerSite.Customer.CustomerName;
bigi.CustomerID = item.First().CustomerSite.Customer.CustomerID;
bigi.NumberOfInspections = item.Count();
bigi.TotalDuration = item.Sum(s => s.Duration);
}
viewModel.BatchInspectionGridViewModel.Add(bigi);
}
}
【问题讨论】:
-
你能再贴一些代码来说明
item是如何创建的吗? -
您在条件表达式中使用 LINQ。可以想象,以前的条件表达式将上下文绑定以进行类似的检查。
-
我猜你正在使用 linq to sql 或 linq to entity 框架。无论哪种方式,您都可以设置与 dbase 的连接以允许多个活动结果集,从而使您能够同时使用多个 DataReader。只需将以下内容添加到您的连接字符串中:MultipleActiveResultSets=true;