从 EF 4.1 代码首先映射时打开 DataReader 和 ValueInjecter
我将Entity Framework 4.1代码优先
与ASP.NET MVC 3
和Razor view
和ValueInjecter
一起使用。
我的视图模型:
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.
我将如何解决此问题?
I am using Entity Framework 4.1 code first
with ASP.NET MVC 3
and Razor view
and ValueInjecter
.
My view model:
public class ProductViewModel
{
public int Id { get; set; }
public string SKU { get; set; }
public string Name { get; set; }
public ICollection<Specification> Specifications { get; set; }
}
Model class:
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; }
}
My action method where I return a list of products and then I need to map each product to a view model.
public ActionResult JsonGetProductList()
{
IEnumerable<Product> productList = productService.GetAll();
// Mapping
IList<ProductViewModel> viewModelList = productList.Select(c => new ProductViewModel().InjectFrom(c)).Cast<ProductViewModel>().ToList();
}
It is giving errors on the mapping part with the following error:
There is already an open DataReader associated with this Command which must be closed first.
How would I fix this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当您在原始查询的 DataReader 关闭之前延迟加载结果的属性时,会出现该错误。
您可以在 GetAll 之后调用 ToList 方法来强制执行完整的查询,也可以将 MultipleActiveResultSets 配置添加到连接字符串以允许多个 DataReader,如下所示:
connectionString="data source=YourServer;Integrated Security=SSPI;
初始目录=YourDB;MultipleActiveResultSets=true"
That error appears when you Lazy Load a property of the results before the DataReader of the original query get closed.
You can call the ToList method after the GetAll to force the complete query execution or you can add the MultipleActiveResultSets config to your connection string to allow multiple DataReaders like this:
connectionString="data source=YourServer;Integrated Security=SSPI;
Initial Catalog=YourDB;MultipleActiveResultSets=true"