从 EF 4.1 代码首先映射时打开 DataReader 和 ValueInjecter

发布于 2024-12-15 05:20:15 字数 1175 浏览 3 评论 0原文

我将Entity Framework 4.1代码优先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.

我将如何解决此问题?

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

瘫痪情歌 2024-12-22 05:20:15

当您在原始查询的 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"

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文