已经有一个打开的 DataReader 与此命令关联,必须先在 Nopcommerce 中关闭

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

我正在从该服务获取产品图片,但在版本 3.8 中出现此错误:

var defaultProductPicture=_pictureService.GetPicturesByProductId(productmodel.Id,1).FirstOrDefault();
productmodel.productImagUrl = _pictureService.GetPictureUrl(defaultProductPicture, 75, true);

我的 GetPicturesByProductId 服务是:

public virtual IList<Picture> GetPicturesByProductId(int productId, int recordsToReturn = 0)
{
    if (productId == 0)
        return new List<Picture>();


    var query = from p in _pictureRepository.Table
                join pp in _productPictureRepository.Table on p.Id equals pp.PictureId
                orderby pp.DisplayOrder
                where pp.ProductId == productId
                select p;

    if (recordsToReturn > 0)
        query = query.Take(recordsToReturn);

    var pics = query.ToList();
    return pics;
}

我不知道为什么 GetPictureByProductId 在较新的 3.8 版本中会遇到此错误。在 3.7 版中一切正常。

如果您在迭代另一个查询的结果时执行查询,就会发生这种情况。

所以只需将 MultipleActiveResultSets=True 添加到 setting.txt 文件中的连接字符串即可。

希望对您有所帮助!