Entity Framework - 我应该在函数中编辑对象,还是在函数完成后编辑

Entity Framework - Should I edit an object in a function, or after a function completes

我正在编写一个 MVC 5 互联网应用程序,我在其中检索许多 Account 需要发送电子邮件的对象,然后发送电子邮件。电子邮件发送后,我需要更新每个 Account 对象中的 DateTime 字段以存储一个值以表明电子邮件已发送。

这是我的代码:

public async Task SendDailyExpirationEmails(int dayInterval)
{
    IEnumerable<Account> freeTrialAccounts = GetFreeTrialAccountsForSendDailyExpirationEmails(dayInterval).ToList();
    IEnumerable<Account> paidServiceAccounts = GetPaidServiceAccountsForSendDailyExpirationEmails(dayInterval).ToList();
    await SendFreeTrialSubscriptionExpirationEmails(freeTrialAccounts);
    await SendPaidSubscriptionExpirationEmails(paidServiceAccounts);
}

SendEmail 函数,对于 freeTrialAccountspaidServiceAccounts,使用 ForEach Loop 遍历 IEnumerable 中的每个 Account

我的问题是:

我应该在 SendEmail 函数完成后更新 DateTime 字段还是在 SendEmail 函数内更新?

是否有针对这种情况的通用编码实践?

提前致谢。

为了保持 DateTime 值的精度,使其在减少数据库调用的同时尽可能正确,您将希望在电子邮件发送后立即记录它,但要等到您的电子邮件发送时才保留信息过程已完成。

我想说有一个 class 属性 跟踪每封电子邮件的发送时间,然后在发送完所有电子邮件后,调用数据库以更新已发送的date/time(s).

也就是说,如果您有其他 job/task/application 尽快依赖该信息,那么您将需要在发送电子邮件后立即保留数据。否则我认为延迟它没有问题。