{"record":{"id":"bb476058a2b02b80","repo":"nopSolutions/nopCommerce","slug":"primary-exchange-rate-currency-cannot-be-loaded","errorCode":null,"errorMessage":"Primary exchange rate currency cannot be loaded","messagePattern":"Primary exchange rate currency cannot be loaded","errorType":"exception","errorClass":"Exception","httpStatus":null,"severity":"error","filePath":"src/Libraries/Nop.Services/Directory/CurrencyService.cs","lineNumber":245,"sourceCode":"        result = await ConvertToPrimaryExchangeRateCurrencyAsync(result, sourceCurrencyCode);\r\n        result = await ConvertFromPrimaryExchangeRateCurrencyAsync(result, targetCurrencyCode);\r\n        return result;\r\n    }\r\n\r\n    /// <summary>\r\n    /// Converts to primary exchange rate currency \r\n    /// </summary>\r\n    /// <param name=\"amount\">Amount</param>\r\n    /// <param name=\"sourceCurrencyCode\">Source currency code</param>\r\n    /// <returns>\r\n    /// A task that represents the asynchronous operation\r\n    /// The task result contains the converted value\r\n    /// </returns>\r\n    public virtual async Task<decimal> ConvertToPrimaryExchangeRateCurrencyAsync(decimal amount, Currency sourceCurrencyCode)\r\n    {\r\n        ArgumentNullException.ThrowIfNull(sourceCurrencyCode);\r\n\r\n        var primaryExchangeRateCurrency = await GetCurrencyByIdAsync(_currencySettings.PrimaryExchangeRateCurrencyId) ?? throw new Exception(\"Primary exchange rate currency cannot be loaded\");\r\n\r\n        var result = amount;\r\n        if (result == decimal.Zero || sourceCurrencyCode.Id == primaryExchangeRateCurrency.Id)\r\n            return result;\r\n\r\n        var exchangeRate = sourceCurrencyCode.Rate;\r\n        if (exchangeRate == decimal.Zero)\r\n            throw new NopException($\"Exchange rate not found for currency [{sourceCurrencyCode.Name}]\");\r\n        result /= exchangeRate;\r\n\r\n        return result;\r\n    }\r\n\r\n    /// <summary>\r\n    /// Converts from primary exchange rate currency\r\n    /// </summary>\r\n    /// <param name=\"amount\">Amount</param>\r\n    /// <param name=\"targetCurrencyCode\">Target currency code</param>\r","sourceCodeStart":227,"sourceCodeEnd":263,"githubUrl":"https://github.com/nopSolutions/nopCommerce/blob/64bdf2ff08c8b39e65717bcf974fb43dc2ef68f2/src/Libraries/Nop.Services/Directory/CurrencyService.cs#L227-L263","documentation":"Thrown by ConvertToPrimaryExchangeRateCurrencyAsync as a plain Exception when GetCurrencyByIdAsync(PrimaryExchangeRateCurrencyId) returns null. The primary exchange-rate currency must exist for any currency conversion to the base currency.","triggerScenarios":"Calling ConvertToPrimaryExchangeRateCurrencyAsync(amount, sourceCurrency) when PrimaryExchangeRateCurrencyId references a deleted/non-existent currency row.","commonSituations":"The primary exchange-rate currency record was deleted from the DB but the setting still points to its old Id; corrupted/inconsistent settings after a migration.","solutions":["Re-point PrimaryExchangeRateCurrencyId to a valid, existing currency in Admin > Configuration > Currencies.","Verify the currency row exists: check GetCurrencyByIdAsync(id) returns non-null in admin tooling.","Add a startup guard that validates PrimaryExchangeRateCurrencyId resolves to a live currency."],"exampleFix":"// before\nvar value = await _currencyService.ConvertToPrimaryExchangeRateCurrencyAsync(amount, source);\n\n// after\nif (await _currencyService.GetCurrencyByIdAsync(_currencySettings.PrimaryExchangeRateCurrencyId) is null)\n    throw new InvalidOperationException(\"Primary exchange rate currency misconfigured.\");\nvar value = await _currencyService.ConvertToPrimaryExchangeRateCurrencyAsync(amount, source);","handlingStrategy":"validation","validationCode":"if (await _currencyService.GetCurrencyByIdAsync(_currencySettings.PrimaryExchangeRateCurrencyId) is null)\n    throw new InvalidOperationException(\"Primary exchange rate currency misconfigured.\");","typeGuard":null,"tryCatchPattern":"try { await _currencyService.ConvertToPrimaryExchangeRateCurrencyAsync(amount, src); }\ncatch (Exception ex) when (ex.Message.Contains(\"Primary exchange rate currency cannot be loaded\"))\n{ /* alert ops to fix currency setting */ }","preventionTips":["Never delete a currency that is referenced by PrimaryExchangeRateCurrencyId.","Add a startup health check verifying the setting resolves.","Re-point the setting after currency maintenance."],"tags":["currency","exchange-rate","configuration","settings"],"backgroundTag":null,"analyzedSha":"64bdf2ff08c8b39e65717bcf974fb43dc2ef68f2","analyzedAt":"2026-08-13T21:19:38.062Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}