2.2 - Exception when Showing number of products besides each category

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.
12 years ago
If "Show the number of distinct products besides each category" is checked, then I get below error when selecting a leaf node in category tree (my categories are 3  levels deep)

It appears as this is being called for the leaf node, and then all siblings of the leaf node too.
exec ProductLoadAllPaged
(Only occurs if showing number of products)

-----------
An object with the same key already exists in the ObjectStateManager. The ObjectStateManager cannot track multiple objects with the same key.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.InvalidOperationException: An object with the same key already exists in the ObjectStateManager. The ObjectStateManager cannot track multiple objects with the same key.

Source Error:


Line 121:                    var result = context.Translate<TEntity>(reader).ToList();
Line 122:                    foreach (var entity in result)
Line 123:                        Set<TEntity>().Attach(entity);
Line 124:                    //close up the reader, we're done saving results
Line 125:                    reader.Close();

Source File: [ ... ] src\Libraries\Nop.Data\NopObjectContext.cs    Line: 123

Stack Trace:


[InvalidOperationException: An object with the same key already exists in the ObjectStateManager. The ObjectStateManager cannot track multiple objects with the same key.]
   System.Data.Objects.ObjectContext.VerifyRootForAdd(Boolean doAttach, String entitySetName, IEntityWrapper wrappedEntity, EntityEntry existingEntry, EntitySet& entitySet, Boolean& isNoOperation) +8607188
   System.Data.Objects.ObjectContext.AttachTo(String entitySetName, Object entity) +194
   System.Data.Entity.Internal.Linq.<>c__DisplayClass2.<Attach>b__1() +86
   System.Data.Entity.Internal.Linq.InternalSet`1.ActOnSet(Action action, EntityState newState, Object entity, String methodName) +341
   System.Data.Entity.Internal.Linq.InternalSet`1.Attach(Object entity) +228
   System.Data.Entity.DbSet`1.Attach(TEntity entity) +246
   Nop.Data.NopObjectContext.ExecuteStoredProcedureList(String commandText, Object[] parameters) in C:\Users\Dennis\Documents\Visual Studio 2010\Projects\nopCommerce2\src\Libraries\Nop.Data\NopObjectContext.cs:123
   Nop.Services.Catalog.ProductService.SearchProducts(Int32 categoryId, Int32 manufacturerId, Nullable`1 featuredProducts, Nullable`1 priceMin, Nullable`1 priceMax, Int32 productTagId, String keywords, Boolean searchDescriptions, Int32 languageId, IList`1 filteredSpecs, ProductSortingEnum orderBy, Int32 pageIndex, Int32 pageSize, Boolean showHidden) in C:\Users\Dennis\Documents\Visual Studio 2010\Projects\nopCommerce2\src\Libraries\Nop.Services\Catalog\ProductService.cs:291
   Nop.Web.Controllers.CatalogController.GetNumberOfProducts(Category category, Boolean includeSubCategories) in C:\Users\Dennis\Documents\Visual Studio 2010\Projects\nopCommerce2\src\Presentation\Nop.Web\Controllers\CatalogController.cs:332
   Nop.Web.Controllers.CatalogController.GetChildCategoryNavigationModel(IList`1 breadCrumb, Int32 rootCategoryId, Category currentCategory, Int32 level) in C:\Users\Dennis\Documents\Visual Studio 2010\Projects\nopCommerce2\src\Presentation\Nop.Web\Controllers\CatalogController.cs:365
   Nop.Web.Controllers.CatalogController.GetChildCategoryNavigationModel(IList`1 breadCrumb, Int32 rootCategoryId, Category currentCategory, Int32 level) in C:\Users\Dennis\Documents\Visual Studio 2010\Projects\nopCommerce2\src\Presentation\Nop.Web\Controllers\CatalogController.cs:371
   Nop.Web.Controllers.CatalogController.GetChildCategoryNavigationModel(IList`1 breadCrumb, Int32 rootCategoryId, Category currentCategory, Int32 level) in C:\Users\Dennis\Documents\Visual Studio 2010\Projects\nopCommerce2\src\Presentation\Nop.Web\Controllers\CatalogController.cs:371
   Nop.Web.Controllers.CatalogController.CategoryNavigation(Int32 currentCategoryId) in C:\Users\Dennis\Documents\Visual Studio 2010\Projects\nopCommerce2\src\Presentation\Nop.Web\Controllers\CatalogController.cs:821
   lambda_method(Closure , ControllerBase , Object[] ) +150
12 years ago
Am I the only one who has this issue?  Would someone else turn on "Show number of products..." in 2.2 and let me know?
12 years ago
Hello,

I've the same problem but with differences if "Include subcategories (number of distinct products):" option is checked or not.

If only "Show the number of distinct products besides each category" the exception occurs only when I select sub-category.

If both are checked, I can not display home page.
12 years ago
You're absolutely right. The issue is 'NopObjectContext' class ('ExecuteStoredProcedureList' method). After entity is read from the database, it's attached to the context: Set<TEntity>().Attach(entity); (line 123). We should ensure that it's not already attached. I don't know how to fix it right now, but I'll investigate it a bit later

UPDATE: But you can simply comment the following source and it'll work:
foreach (var entity in result)
   Set<TEntity>().Attach(entity);
12 years ago
Same error occurs when just "Show the number of distinct products besides each category" is selected while "Include subcategories (number of distinct products)" is not selected AND now you step to the public store and click on one of the sample "SUB-Categories".
When you just click a main category, the error does not occur.

Otherwise: I love this shop system. =)
12 years ago
Fixed. Please see changeset 9da05e4bc0e2
12 years ago
Is there a build with this fix? I do not have VS2010 to compile and the deploy script will not build due to errors.


Build FAILED.

       "C:\Users\luser\Downloads\nopcommerce-9da05e4bc0e2\nopCommerce_9da05e4bc0e2\src\build\nop.proj" (default target) (1) ->
       "C:\Users\luser\Downloads\nopcommerce-9da05e4bc0e2\nopCommerce_9da05e4bc0e2\src\NopCommerce.sln" (default target) (2) ->
       "C:\Users\luser\Downloads\nopcommerce-9da05e4bc0e2\nopCommerce_9da05e4bc0e2\src\Libraries\Nop.Core\Nop.Core.csproj" (default target)
        (3) ->
       (ResolveAssemblyReferences target) ->
         c:\Windows\Microsoft.NET\Framework\v4.0.30319\Microsoft.Common.targets(1360,9): warning MSB3245: Could not resolve this reference.
        Could not locate the assembly "System.Web.Mvc". Check to make sure the assembly exists on disk. If this reference is required by your code, you may get compilation errors. [C:\Users\luser\Downloads\nopcommerce-9da05e4bc0e2\nopCommerce_9da05e4bc0e2\src\Libraries\N
       op.Core\Nop.Core.csproj]


       "C:\Users\luser\Downloads\nopcommerce-9da05e4bc0e2\nopCommerce_9da05e4bc0e2\src\build\nop.proj" (default target) (1) ->
       "C:\Users\luser\Downloads\nopcommerce-9da05e4bc0e2\nopCommerce_9da05e4bc0e2\src\NopCommerce.sln" (default target) (2) ->
       "C:\Users\luser\Downloads\nopcommerce-9da05e4bc0e2\nopCommerce_9da05e4bc0e2\src\Presentation\Nop.Web\Nop.Web.csproj" (default target
       ) (24) ->
         C:\Users\luser\Downloads\nopcommerce-9da05e4bc0e2\nopCommerce_9da05e4bc0e2\src\Presentation\Nop.Web\Nop.Web.csproj(1297,3): error
       MSB4019: The imported project "C:\Program Files\MSBuild\Microsoft\VisualStudio\v10.0\WebApplications\Microsoft.WebApplication.target
       s" was not found. Confirm that the path in the <Import> declaration is correct, and that the file exists on disk.


       "C:\Users\luser\Downloads\nopcommerce-9da05e4bc0e2\nopCommerce_9da05e4bc0e2\src\build\nop.proj" (default target) (1) ->
       "C:\Users\luser\Downloads\nopcommerce-9da05e4bc0e2\nopCommerce_9da05e4bc0e2\src\NopCommerce.sln" (default target) (2) ->
       "C:\Users\luser\Downloads\nopcommerce-9da05e4bc0e2\nopCommerce_9da05e4bc0e2\src\Presentation\Nop.Web\Administration\Nop.Admin.csproj
       " (default target) (25) ->
         C:\Users\luser\Downloads\nopcommerce-9da05e4bc0e2\nopCommerce_9da05e4bc0e2\src\Presentation\Nop.Web\Administration\Nop.Admin.cspro
       j(960,3): error MSB4019: The imported project "C:\Program Files\MSBuild\Microsoft\VisualStudio\v10.0\WebApplications\Microsoft.WebApplication.targets" was not found. Confirm that the path in the <Import> declaration is correct, and that the file exists on disk.

       "C:\Users\luser\Downloads\nopcommerce-9da05e4bc0e2\nopCommerce_9da05e4bc0e2\src\build\nop.proj" (default target) (1) ->
       "C:\Users\luser\Downloads\nopcommerce-9da05e4bc0e2\nopCommerce_9da05e4bc0e2\src\NopCommerce.sln" (default target) (2) ->
       "C:\Users\luser\Downloads\nopcommerce-9da05e4bc0e2\nopCommerce_9da05e4bc0e2\src\Libraries\Nop.Core\Nop.Core.csproj" (default target)
        (3) ->
       (CoreCompile target) ->
         Fakes\FakeHttpRequest.cs(4,18): error CS0234: The type or namespace name 'Mvc' does not exist in the namespace 'System.Web' (are you missing an assembly reference?) [C:\Users\luser\Downloads\nopcommerce-9da05e4bc0e2\nopCommerce_9da05e4bc0e2\src\Libraries\Nop.Core\Nop.Core.csproj]

    1 Warning(s)
    3 Error(s)
12 years ago
dmcmullenii wrote:
Is there a build with this fix? I do not have VS2010 to compile and the deploy script will not build due to errors.


Build FAILED.

       "C:\Users\luser\Downloads\nopcommerce-9da05e4bc0e2\nopCommerce_9da05e4bc0e2\src\build\nop.proj" (default target) (1) ->
       "C:\Users\luser\Downloads\nopcommerce-9da05e4bc0e2\nopCommerce_9da05e4bc0e2\src\NopCommerce.sln" (default target) (2) ->
       "C:\Users\luser\Downloads\nopcommerce-9da05e4bc0e2\nopCommerce_9da05e4bc0e2\src\Libraries\Nop.Core\Nop.Core.csproj" (default target)
        (3) ->
       (ResolveAssemblyReferences target) ->
         c:\Windows\Microsoft.NET\Framework\v4.0.30319\Microsoft.Common.targets(1360,9): warning MSB3245: Could not resolve this reference.
        Could not locate the assembly "System.Web.Mvc". Check to make sure the assembly exists on disk. If this reference is required by your code, you may get compilation errors. [C:\Users\luser\Downloads\nopcommerce-9da05e4bc0e2\nopCommerce_9da05e4bc0e2\src\Libraries\N
       op.Core\Nop.Core.csproj]


       "C:\Users\luser\Downloads\nopcommerce-9da05e4bc0e2\nopCommerce_9da05e4bc0e2\src\build\nop.proj" (default target) (1) ->
       "C:\Users\luser\Downloads\nopcommerce-9da05e4bc0e2\nopCommerce_9da05e4bc0e2\src\NopCommerce.sln" (default target) (2) ->
       "C:\Users\luser\Downloads\nopcommerce-9da05e4bc0e2\nopCommerce_9da05e4bc0e2\src\Presentation\Nop.Web\Nop.Web.csproj" (default target
       ) (24) ->
         C:\Users\luser\Downloads\nopcommerce-9da05e4bc0e2\nopCommerce_9da05e4bc0e2\src\Presentation\Nop.Web\Nop.Web.csproj(1297,3): error
       MSB4019: The imported project "C:\Program Files\MSBuild\Microsoft\VisualStudio\v10.0\WebApplications\Microsoft.WebApplication.target
       s" was not found. Confirm that the path in the <Import> declaration is correct, and that the file exists on disk.


       "C:\Users\luser\Downloads\nopcommerce-9da05e4bc0e2\nopCommerce_9da05e4bc0e2\src\build\nop.proj" (default target) (1) ->
       "C:\Users\luser\Downloads\nopcommerce-9da05e4bc0e2\nopCommerce_9da05e4bc0e2\src\NopCommerce.sln" (default target) (2) ->
       "C:\Users\luser\Downloads\nopcommerce-9da05e4bc0e2\nopCommerce_9da05e4bc0e2\src\Presentation\Nop.Web\Administration\Nop.Admin.csproj
       " (default target) (25) ->
         C:\Users\luser\Downloads\nopcommerce-9da05e4bc0e2\nopCommerce_9da05e4bc0e2\src\Presentation\Nop.Web\Administration\Nop.Admin.cspro
       j(960,3): error MSB4019: The imported project "C:\Program Files\MSBuild\Microsoft\VisualStudio\v10.0\WebApplications\Microsoft.WebApplication.targets" was not found. Confirm that the path in the <Import> declaration is correct, and that the file exists on disk.

       "C:\Users\luser\Downloads\nopcommerce-9da05e4bc0e2\nopCommerce_9da05e4bc0e2\src\build\nop.proj" (default target) (1) ->
       "C:\Users\luser\Downloads\nopcommerce-9da05e4bc0e2\nopCommerce_9da05e4bc0e2\src\NopCommerce.sln" (default target) (2) ->
       "C:\Users\luser\Downloads\nopcommerce-9da05e4bc0e2\nopCommerce_9da05e4bc0e2\src\Libraries\Nop.Core\Nop.Core.csproj" (default target)
        (3) ->
       (CoreCompile target) ->
         Fakes\FakeHttpRequest.cs(4,18): error CS0234: The type or namespace name 'Mvc' does not exist in the namespace 'System.Web' (are you missing an assembly reference?) [C:\Users\luser\Downloads\nopcommerce-9da05e4bc0e2\nopCommerce_9da05e4bc0e2\src\Libraries\Nop.Core\Nop.Core.csproj]

    1 Warning(s)
    3 Error(s)


Yes its fixed..
12 years ago
It will be easy for a developer, if you say which file, we need to change in the original source, since we already have changed some controller classes.
12 years ago
[email protected] wrote:
It will be easy for a developer, if you say which file, we need to change in the original source, since we already have changed some controller classes.

Just click on the link I provided above. It contains a list of changed files and appropriate changes
This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.