{"record":{"id":"8c65867b0327950c","repo":"nopSolutions/nopCommerce","slug":"tname-template-could-not-be-loaded","errorCode":null,"errorMessage":"\"{tName}\" template could not be loaded","messagePattern":"\"(.+?)\" template could not be loaded","errorType":"exception","errorClass":"Exception","httpStatus":null,"severity":"error","filePath":"src/Libraries/Nop.Services/Installation/InstallSampleData.Products.cs","lineNumber":127,"sourceCode":"        async Task<int> getAndSaveId(Dictionary<string, int> dict, string key, Func<string, Task<int>> foo)\n        {\n            if (string.IsNullOrEmpty(key))\n                return 0;\n\n            if (dict.TryGetValue(key, out var id))\n                return id;\n\n            id = await foo(key);\n            dict[key] = id;\n\n            return id;\n        }\n\n        async Task<int> getProductTemplate(string templateName)\n        {\n            return await getAndSaveId(productTemplates, templateName,\n                async tName => await GetFirstEntityIdAsync<ProductTemplate>(pt => pt.Name == tName) ??\n                    throw new Exception($\"\\\"{tName}\\\" template could not be loaded\"));\n        }\n\n        async Task<int> getTaxCategoryId(string taxCategoryName)\n        {\n            return await getAndSaveId(taxCategories, taxCategoryName, async tcName => await GetFirstEntityIdAsync<TaxCategory>(tc => tc.Name == tcName) ??\n                throw new Exception($\"\\\"{tcName}\\\" tax category could not be loaded\"));\n        }\n\n        async Task<int> getCategoryId(string categoryName)\n        {\n            return await getAndSaveId(categories, categoryName, async cName => await GetFirstEntityIdAsync<Category>(c => c.Name == cName) ??\n                throw new Exception($\"\\\"{cName}\\\" category could not be loaded\"));\n        }\n\n        async Task<int> getManufacturerId(string manufacturerName)\n        {\n            return await getAndSaveId(manufacturers, manufacturerName, async mName => await GetFirstEntityIdAsync<Manufacturer>(m => m.Name == mName) ??\n                throw new Exception($\"\\\"{mName}\\\" manufacturer could not be loaded\"));","sourceCodeStart":109,"sourceCodeEnd":145,"githubUrl":"https://github.com/nopSolutions/nopCommerce/blob/64bdf2ff08c8b39e65717bcf974fb43dc2ef68f2/src/Libraries/Nop.Services/Installation/InstallSampleData.Products.cs#L109-L145","documentation":"Thrown by getProductTemplate inside sample-product installation. It caches entity IDs by name via getAndSaveId; if GetFirstEntityIdAsync<ProductTemplate> returns null for the given template name, no product can reference that template, so it throws. This means a referenced product template name was never seeded, indicating sample data prerequisites (the base install that creates templates) were skipped or mismatch.","triggerScenarios":"Installing sample product data that references a ProductTemplate by name when that template does not exist; running sample-data install without the base installation that seeds product templates; a typo in the template name in the sample data.","commonSituations":"Sample data installed against an incomplete base install; template names changed between versions; a custom sample-data file using template names not present in the DB.","solutions":["Run the base installer (which seeds product templates) before installing sample product data.","Verify the ProductTemplate names referenced in the sample data exist in the DB.","Fix typos in template names in the sample-data source.","Reinstall from a clean database to ensure prerequisites are present."],"exampleFix":"// before\nreturn await getAndSaveId(productTemplates, templateName,\n    async tName => await GetFirstEntityIdAsync<ProductTemplate>(pt => pt.Name == tName)\n        ?? throw new Exception($\"\\\"{tName}\\\" template could not be loaded\"));\n\n// after - list available templates on failure to aid debugging\nvar id = await GetFirstEntityIdAsync<ProductTemplate>(pt => pt.Name == templateName);\nif (id == 0)\n    throw new InvalidOperationException($\"Product template '{templateName}' not found. Seed product templates first.\");","handlingStrategy":"validation","validationCode":"// Verify each referenced product template name exists before sample install\nvar needed = new[] { \"Simple product\", \"Grouped product\" };\nvar missing = needed.Except(await Table<ProductTemplate>().Select(pt => pt.Name).ToListAsync());\nif (missing.Any())\n    throw new InvalidOperationException($\"Missing product templates: {string.Join(\", \", missing)}\");","typeGuard":"static async Task<bool> ProductTemplateExistsAsync(IRepository<ProductTemplate> repo, string name)\n    => await repo.Table.AnyAsync(pt => pt.Name == name);","tryCatchPattern":"try { await getProductTemplate(templateName); }\ncatch (Exception ex) when (ex.Message.Contains(\"template could not be loaded\"))\n{ /* log the missing name and the available templates, then abort sample install */ }","preventionTips":["Run the base install (which seeds product templates) before sample data.","Keep template names in sample data in sync with seeded names.","Reinstall from a clean DB."],"tags":["installation","sample-data","product-template","seeding","nopcommerce"],"backgroundTag":null,"analyzedSha":"64bdf2ff08c8b39e65717bcf974fb43dc2ef68f2","analyzedAt":"2026-08-13T21:19:38.062Z","schemaVersion":2},"datasetVersion":"2026-08-14T05:17:29.042Z"}