EF Core两次提取相同的数据

时间:2020-10-20 15:09:30

标签: c# razor-pages asp.net-core-2.2 ef-core-2.2

我有一个向我的系统成员发送电子邮件的功能。他们将收到哪种电子邮件取决于不同的条件。

最近我发现,它有时会两次发送给同一个人。

我的代码-简化:

In my startup.cs:

[...]

services.AddDbContext<DBContext>(options => options.UseSqlServer(Configuration.GetConnectionString("App")));

[...]


My page:

namespace MyApp.Pages
{

    public class MemberMailsModel : PageModel
    {

        private readonly DBContext _dbContext;
        private readonly IHttpClientFactory _httpClientFactory;

        public MemberMailsModel(DBContext dbContext, IHttpClientFactory httpClientFactory)
        {
            _dbContext = dbContext;
            _httpClientFactory = httpClientFactory;
        }

        public async Task<PageResult> OnGetAsync()
        {

            var httpClient = _httpClientFactory.CreateClient("SendInBlue");
            var members = await _dbContext.Members.Include(x => x.Logs).Where(x => x.ConsentDate == null).AsNoTracking().ToListAsync();

            foreach (var member in members)
            {

                bool mail1sent = member.Logs.Any(x => x.Action == "Mail1Sent");
                bool mail2sent = member.Logs.Any(x => x.Action == "Mail2Sent");
                bool mail3sent = member.Logs.Any(x => x.Action == "Mail3Sent");

                if (!mail1sent)
                {                
                    var sibMail = await httpClient.PostAsync(...);
                    sibMail.Dispose();

                    var log = new Log
                    {
                        Date = DateTime.Now,
                        Action = "Mail1Sent",
                        Text = "Mail #1 sent",
                        MemberId = member.Id
                    }
                    _dbContext.Add(log);
                }
                else if (!mail2sent && mail1sent)
                {                
                    var sibMail = await httpClient.PostAsync(...);
                    sibMail.Dispose();

                    var log = new Log
                    {
                        Date = DateTime.Now,
                        Action = "Mail2Sent",
                        Text = "Mail #2 sent",
                        MemberId = member.Id
                    }
                    _dbContext.Add(log);
                }
                else if (!mail3sent && mail2sent)
                {                
                    var sibMail = await httpClient.PostAsync(...);
                    sibMail.Dispose();

                    var log = new Log
                    {
                        Date = DateTime.Now,
                        Action = "Mail3Sent",
                        Text = "Mail #3 sent",
                        MemberId = member.Id
                    }
                    _dbContext.Add(log);
                }

                await _dbContext.SaveChangesAsync();

            }

            return Page();

        }

    }

}

我可以在自定义日志功能中看到,某些成员两次获得相同的电子邮件,并在完全相同的时间戳下两次被添加到日志中,就像该成员两次在查询中一样。

它可以在任意随机成员的任何条件下发生。

那怎么可能?

0 个答案:

没有答案