1

我正在Entity Framework 4.1 code first使用ASP.NET MVC 3Razor viewValueInjecter

我的视图模型:

public class ProductViewModel
{
     public int Id { get; set; }
     public string SKU { get; set; }
     public string Name { get; set; }
     public ICollection<Specification> Specifications { get; set; }
}

型号类:

public class Product : IEntity
{
     public int Id { get; set; }
     public string SKU { get; set; }
     public string Name { get; set; }
     public virtual ICollection<Specification> Specifications { get; set; }
}

我的操作方法返回产品列表,然后我需要将每个产品映射到视图模型。

public ActionResult JsonGetProductList()
{
     IEnumerable<Product> productList = productService.GetAll();

     // Mapping
     IList<ProductViewModel> viewModelList = productList.Select(c => new ProductViewModel().InjectFrom(c)).Cast<ProductViewModel>().ToList();
}

它在映射部分给出错误,并出现以下错误:

There is already an open DataReader associated with this Command which must be closed first.

我将如何解决这个问题?

4

1 回答 1

2

当您在原始查询的 DataReader 关闭之前延迟加载结果的属性时,会出现该错误。您可以在 GetAll 之后调用 ToList 方法来强制执行完整的查询,或者您可以将 MultipleActiveResultSets 配置添加到您的连接字符串以允许多个 DataReader,如下所示:

connectionString="data source=YourServer;Integrated Security=SSPI; Initial Catalog=YourDB;MultipleActiveResultSets=true"

于 2011-11-12T13:12:46.077 回答