Ironically, I ran into the same problem. My client had me write code to randomly pick the category picture based on one of the product pictures within that category. I'm running nop 2.2.
The error I am getting is:
An object with the same key already exists in the ObjectStateManager. The ObjectStateManager cannot track multiple objects with the same key.
If I comment out the lines for Set<TEntity>().Attach(entity); at the bottom of Libraries/nopData/nopObjectContext.cs, the problem goes away. I'm trying to figure out what the Set<TEntity> is needed since I just want to get the result list.
var result = context.Translate<TEntity>(reader).ToList();
// LJD 12/22/2011 not sure what the following lines do
//foreach (var entity in result)
// Set<TEntity>().Attach(entity);
//close up the reader, we're done saving results
reader.Close();
return result;
}
}
}
}
}
BTW: The code I wrote for the Random Category Picture is in Category Action Result in the Category Controller:
// LJD 12/22/2011 If x.PictureId > 0, get the picture, otherwise see if you can randomly find a product picture for that category
if (x.PictureId > 0)
{
subCatModel.PictureModel.ImageUrl = _pictureService.GetPictureUrl(x.PictureId, _mediaSetting.CategoryThumbPictureSize, true);
}
else
{
var categoryProducts = _productService.SearchProducts(x.Id, 0, null, null, null, 0, string.Empty, false, 0, null, ProductSortingEnum.Position, 0, int.MaxValue);
var numberOfProducts = categoryProducts.TotalCount;
if (numberOfProducts <= 0)
{
subCatModel.PictureModel.ImageUrl = _pictureService.GetPictureUrl(x.PictureId, _mediaSetting.CategoryThumbPictureSize, true);
}
else
{
var iRandomProductForCategory = CommonHelper.GenerateRandomInteger(0, numberOfProducts);
var picture = _pictureService.GetPicturesByProductId(categoryProducts[iRandomProductForCategory].Id, 1).FirstOrDefault();
subCatModel.PictureModel.ImageUrl = _pictureService.GetPictureUrl(picture, _mediaSetting.CategoryThumbPictureSize, true);
}
}
subCatModel.PictureModel.Title = string.Format(_localizationService.GetResource("Media.Category.ImageLinkTitleFormat"), model.Name);
subCatModel.PictureModel.AlternateText = string.Format(_localizationService.GetResource("Media.Category.ImageAlternateTextFormat"), model.Name);
return subCatModel;