nopSolutions/nopCommerce · warning · NopException

Category not found

Error message

Category not found

What it means

Thrown while resolving a menu item of type Category in InitMenuItemModelEntityIdAsync. The factory loads the category by EntityId and, if it is null OR its Deleted flag is true, throws NopException("Category not found"). The exception is caught at line 182 and shown as a warning notification. It guards against both a missing category record and a soft-deleted category, since nopCommerce categories use an IsDeleted (Deleted) flag rather than physical deletion.

Source

Thrown at src/Presentation/Nop.Web/Areas/Admin/Factories/MenuModelFactory.cs:153

                    if (product is null || product.Deleted)
                        throw new NopException("Product not found");

                    model.ProductName = product.Name;
                    model.ProductId = product.Id;
                    break;
                }
                case MenuItemType.TopicPage:
                {
                    var topic = await _topicService.GetTopicByIdAsync(entityId) ?? throw new NopException("Topic not found");
                    model.TopicId = topic.Id;
                    break;
                }
                case MenuItemType.Category:
                {
                    var category = await _categoryService.GetCategoryByIdAsync(entityId);

                    if (category is null || category.Deleted)
                        throw new NopException("Category not found");

                    model.CategoryId = category.Id;
                    break;
                }
                case MenuItemType.Vendor:
                {
                    var vendor = await _vendorService.GetVendorByIdAsync(entityId);

                    if (vendor is null || vendor.Deleted)
                        throw new NopException("Manufacturer not found");

                    model.VendorId = vendor.Id;
                    break;
                }
                case MenuItemType.Manufacturer:
                {
                    var manufacturer = await _manufacturerService.GetManufacturerByIdAsync(entityId);

View on GitHub (pinned to 64bdf2ff08)

Solutions

  1. Edit the menu item in admin and pick a non-deleted category.
  2. Undelete the category if deletion was unintended (set Deleted = false) or restore from backup.
  3. Add a cleanup step to your category-deletion flow that also removes/reassigns dependent menu items.
  4. Run a query joining MenuItem (Category type) to Category where Category.Deleted = 1 and fix the dangling rows.

Example fix

// before
var category = await _categoryService.GetCategoryByIdAsync(entityId);
if (category is null || category.Deleted)
    throw new NopException("Category not found");
// after (treat soft-deleted same as missing, log entity id)
var category = await _categoryService.GetCategoryByIdAsync(entityId);
if (category is null || category.Deleted)
{
    _notificationService.WarningNotification($"Category {entityId} is missing or deleted.");
    return;
}
Defensive patterns

Strategy: validation

Validate before calling

// Validate category exists and is not soft-deleted before initializing the menu item.
if (model.MenuItemTypeId == (int)MenuItemType.Category)
{
    var category = await _categoryService.GetCategoryByIdAsync(entityId);
    if (category is null || category.Deleted)
    {
        _notificationService.WarningNotification($"Category {entityId} not found or deleted.");
        return;
    }
}

Type guard

static bool CategoryUsable(Category c) => c is not null && !c.Deleted;

Try / catch

try { /* category case */ }
catch (NopException ex) { _notificationService.WarningNotification(ex.Message); }

Prevention

When it happens

Trigger: A Category menu item references a CategoryId that was soft-deleted (Deleted = true) or never existed (null). Triggered by GetCategoryByIdAsync returning a Deleted entity, e.g. after an admin deleted the category but left the menu item in place.

Common situations: Category deleted via admin while still referenced by a custom menu; bulk category deletion scripts that ignore menu references; staging-to-production sync that drops categories but keeps menus; multi-store where the category is scoped to a different store.

Related errors


AI-assisted analysis of nopSolutions/nopCommerce@64bdf2ff08 (2026-08-13). Data as JSON: /api/errors/58943f5ee6da1b06. Report an issue: GitHub.