nopSolutions/nopCommerce · critical · Exception

Default email account cannot be loaded

Error message

Default email account cannot be loaded

What it means

In the UpgradeTo470 migration, when seeding the GDPR 'delete customer request' message template, the code loads the first EmailAccount via _dataProvider.GetTable<EmailAccount>().FirstOrDefault() and throws a bare Exception if none exists. The migration assumes at least one email account is present; an empty EmailAccount table aborts the upgrade.

Source

Thrown at src/Libraries/Nop.Data/Migrations/UpgradeTo470/DataMigration.cs:157

                _dataProvider.UpdateEntitiesAsync(values);

                pageIndex++;
            }
        }

        // new permission
        if (_dataProvider.GetTable<PermissionRecord>().Any(pr => string.Compare(pr.SystemName, "AccessProfiling", StringComparison.InvariantCultureIgnoreCase) == 0)) 
            _dataProvider.BulkDeleteEntitiesAsync<PermissionRecord>(pr => pr.SystemName == "AccessProfiling");

        //#6890
        //remove column
        this.DeleteColumnsIfExists<Product>(["IsTelecommunicationsOrBroadcastingOrElectronicServices"]);

        //New message template
        if (!_dataProvider.GetTable<MessageTemplate>().Any(st => string.Compare(st.Name, MessageTemplateSystemNames.DELETE_CUSTOMER_REQUEST_STORE_OWNER_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.DELETE_CUSTOMER_REQUEST_STORE_OWNER_NOTIFICATION,
                Subject = "%Store.Name%. New request to delete customer (GDPR)",
                Body = $"%Customer.Email% has requested account deletion. You can consider this in the admin area.",
                IsActive = true,
                EmailAccountId = eaGeneral.Id
            });
        }

        //#7031
        var emailAccountTableName = nameof(EmailAccount);
        var credentialsColumnName = "UseDefaultCredentials";

        if (Schema.Table(emailAccountTableName).Column(credentialsColumnName).Exists())
        {
            var emailAccounts = _dataProvider.GetTable<EmailAccount>().ToList();
            foreach (var item in emailAccounts)

View on GitHub (pinned to 64bdf2ff08)

Solutions

  1. Before upgrading, ensure at least one EmailAccount row exists (create the default email account in admin or insert a seed row).
  2. Restore/insert the default email account from a known-good backup before running the migration.
  3. If the table is genuinely empty, add a temporary email account, complete the migration, then adjust as needed.

Example fix

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

// after (pre-migration fix: ensure a row exists)
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.70 migration, confirm an email account exists
if (!_dataProvider.GetTable<EmailAccount>().Any())
    throw new InvalidOperationException("Create at least one EmailAccount before upgrading to 4.70.");

Prevention

When it happens

Trigger: Running the 4.70 data migration on a database whose EmailAccount table is empty, so FirstOrDefault() returns null and the null-coalescing throw fires.

Common situations: Upgrading a database where email accounts were never configured or were all deleted; a partially restored dev database missing seed email accounts; test/staging DB seeded without the default email account.

Related errors


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