nopSolutions/nopCommerce · critical · Exception

Default email account cannot be loaded

Error message

Default email account cannot be loaded

What it means

In the UpgradeTo480 migration, when seeding the 'Order cancelled vendor notification' message template, the code does GetTable<EmailAccount>().FirstOrDefault() and throws a bare Exception if no email account exists. Blocks the 4.80 upgrade.

Source

Thrown at src/Libraries/Nop.Data/Migrations/UpgradeTo480/DataMigration.cs:24

[NopUpdateMigration("2023-10-30 12:00:00", "4.80", UpdateMigrationType.Data)]
public class DataMigration : Migration
{
    private readonly INopDataProvider _dataProvider;

    public DataMigration(INopDataProvider dataProvider)
    {
        _dataProvider = dataProvider;
    }

    /// <summary>
    /// Collect the UP migration expressions
    /// </summary>
    public override void Up()
    {
        //#7108 New message template
        if (!_dataProvider.GetTable<MessageTemplate>().Any(st => string.Compare(st.Name, MessageTemplateSystemNames.ORDER_CANCELLED_VENDOR_NOTIFICATION, StringComparison.InvariantCultureIgnoreCase) == 0))
        {
            var eaGeneral = _dataProvider.GetTable<EmailAccount>().FirstOrDefault() ?? throw new Exception("Default email account cannot be loaded");
            _dataProvider.InsertEntity(new MessageTemplate()
            {
                Name = MessageTemplateSystemNames.ORDER_CANCELLED_VENDOR_NOTIFICATION,
                Subject = "%Store.Name%. Order #%Order.OrderNumber% cancelled",
                Body = $"<p>{Environment.NewLine}<a href=\"%Store.URL%\">%Store.Name%</a>{Environment.NewLine}<br />{Environment.NewLine}<br />{Environment.NewLine}Order #%Order.OrderNumber% has been cancelled.{Environment.NewLine}<br />{Environment.NewLine}Customer: %Order.CustomerFullName%,{Environment.NewLine}<br />{Environment.NewLine}<br />{Environment.NewLine}<br />{Environment.NewLine}Order Number: %Order.OrderNumber%{Environment.NewLine}<br />{Environment.NewLine}Date Ordered: %Order.CreatedOn%{Environment.NewLine}<br />{Environment.NewLine}<br />{Environment.NewLine}%Order.Product(s)%{Environment.NewLine}</p>{Environment.NewLine}",
                IsActive = true,
                EmailAccountId = eaGeneral.Id
            });
        }

        //#5898
        if (!_dataProvider.GetTable<MessageTemplate>().Any(st => string.Compare(st.Name, MessageTemplateSystemNames.QUANTITY_BELOW_VENDOR_NOTIFICATION, StringComparison.InvariantCultureIgnoreCase) == 0))
        {
            var eaGeneral = _dataProvider.GetTable<EmailAccount>().FirstOrDefault() ?? throw new Exception("Default email account cannot be loaded");
            _dataProvider.InsertEntity(new MessageTemplate()
            {
                Name = MessageTemplateSystemNames.QUANTITY_BELOW_VENDOR_NOTIFICATION,
                Subject = "%Store.Name%. Quantity below notification. %Product.Name%\"",

View on GitHub (pinned to 64bdf2ff08)

Solutions

  1. Ensure at least one EmailAccount row exists before running the 4.80 migration.
  2. Insert/restore the default email account from a backup.
  3. Create a temporary email account to satisfy the migration, then reconfigure.

Example fix

// before
var eaGeneral = _dataProvider.GetTable<EmailAccount>().FirstOrDefault() ?? throw new Exception("Default email account cannot be loaded");

// after (pre-upgrade: seed an account)
INSERT INTO EmailAccount (Email, DisplayName, Host, Port, Username, Password, EnableSsl, UseDefaultCredentials, SmtpFields, IsDefaultEmailAccount)
VALUES ('store@example.com','Store','smtp.example.com',25,'user','pwd',1,0,'',1);
Defensive patterns

Strategy: validation

Validate before calling

// Before running the 4.80 migration, confirm an email account exists
if (!_dataProvider.GetTable<EmailAccount>().Any())
    throw new InvalidOperationException("Create at least one EmailAccount before upgrading to 4.80.");

Prevention

When it happens

Trigger: Running the 4.80 migration on a DB with an empty EmailAccount table; FirstOrDefault() returns null and the ?? throw triggers.

Common situations: Upgrading a DB where email accounts were never set up or were deleted; dev/staging DB restored without seed email accounts.

Related errors


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