nopSolutions/nopCommerce · error · Exception

"{cName}" category could not be loaded

Error message

"{cName}" category could not be loaded

What it means

Thrown by getCategoryId during sample-product installation. It resolves a Category by name; if none matches, sample products cannot be assigned to a category, so it throws. This means the referenced category name was not seeded (categories are typically part of base/sample setup) or the sample data uses a name that does not exist.

Source

Thrown at src/Libraries/Nop.Services/Installation/InstallSampleData.Products.cs:139

        }

        async Task<int> getProductTemplate(string templateName)
        {
            return await getAndSaveId(productTemplates, templateName,
                async tName => await GetFirstEntityIdAsync<ProductTemplate>(pt => pt.Name == tName) ??
                    throw new Exception($"\"{tName}\" template could not be loaded"));
        }

        async Task<int> getTaxCategoryId(string taxCategoryName)
        {
            return await getAndSaveId(taxCategories, taxCategoryName, async tcName => await GetFirstEntityIdAsync<TaxCategory>(tc => tc.Name == tcName) ??
                throw new Exception($"\"{tcName}\" tax category could not be loaded"));
        }

        async Task<int> getCategoryId(string categoryName)
        {
            return await getAndSaveId(categories, categoryName, async cName => await GetFirstEntityIdAsync<Category>(c => c.Name == cName) ??
                throw new Exception($"\"{cName}\" category could not be loaded"));
        }

        async Task<int> getManufacturerId(string manufacturerName)
        {
            return await getAndSaveId(manufacturers, manufacturerName, async mName => await GetFirstEntityIdAsync<Manufacturer>(m => m.Name == mName) ??
                throw new Exception($"\"{mName}\" manufacturer could not be loaded"));
        }

        async Task<int> getProductAttributeId(string productAttributeName)
        {
            return await getAndSaveId(productAttributes, productAttributeName, async paName => await GetFirstEntityIdAsync<ProductAttribute>(pa => pa.Name == paName) ??
                throw new Exception($"\"{paName}\" product attribute could not be loaded"));
        }

        async Task<int> getProductAvailabilityRangeId(string productAvailabilityRangeName)
        {
            return await getAndSaveId(productAvailabilityRanges, productAvailabilityRangeName, async parName => await GetFirstEntityIdAsync<ProductAvailabilityRange>(par => par.Name == parName) ??
                throw new Exception($"\"{parName}\" product availability range could not be loaded"));

View on GitHub (pinned to 64bdf2ff08)

Solutions

  1. Run the category-seeding install step before sample product install.
  2. Verify referenced Category names exist before installing sample products.
  3. Fix category-name typos in the sample-data source.
  4. Reinstall from a clean database.

Example fix

// before
async cName => await GetFirstEntityIdAsync<Category>(c => c.Name == cName)
    ?? throw new Exception($"\"{cName}\" category could not be loaded")

// after
var id = await GetFirstEntityIdAsync<Category>(c => c.Name == categoryName);
if (id == 0)
    throw new InvalidOperationException($"Category '{categoryName}' not found. Seed categories first.");
Defensive patterns

Strategy: validation

Validate before calling

// Verify referenced categories exist before sample install
var needed = new[] { "Computers", "Electronics" };
var missing = needed.Except(await Table<Category>().Select(c => c.Name).ToListAsync());
if (missing.Any())
    throw new InvalidOperationException($"Missing categories: {string.Join(", ", missing)}");

Type guard

static async Task<bool> CategoryExistsAsync(IRepository<Category> repo, string name)
    => await repo.Table.AnyAsync(c => c.Name == name);

Try / catch

try { await getCategoryId(name); }
catch (Exception ex) when (ex.Message.Contains("category could not be loaded"))
{ /* log missing name + available categories; abort sample install */ }

Prevention

When it happens

Trigger: Installing sample products that reference a Category name absent from the DB; running product sample install before category seeding; name mismatch between sample data and seeded categories.

Common situations: Partial install missing category seeding; reordering of install steps; a custom sample-data file with wrong category names.

Related errors


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