首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Pomelo.EntityFrameworkCore.MySql:这个MySqlConnection已经在使用了

Pomelo.EntityFrameworkCore.MySql:这个MySqlConnection已经在使用了
EN

Stack Overflow用户
提问于 2022-04-10 18:18:58
回答 1查看 415关注 0票数 0

目前,我正在开发一个使用实体框架和Hangfire的ASP.NET API。到目前一切尚好。在Hangfire后台作业期间,当我试图将更改保存到数据库(使用实体框架中的Pomelo.MySQL连接器)时,我会得到标题中提到的异常。

我的一些代码:

Program.cs

代码语言:javascript
复制
...
if (app.Environment.IsDevelopment())
{
    Jobs.Enqueue();
}
...

Jobs.cs

代码语言:javascript
复制
public static class Jobs
{
        public static void Enqueue()
        {
            RecurringJob.AddOrUpdate<AppointmentList>(x => x.BackgroundJob(), Conversion.Cron(0));
        }
}

AppointmentList.cs

代码语言:javascript
复制
public class AppointmentList
{
        private readonly IMailService _mailService;
        private readonly INotificationService _notificationService;


        public AppointmentList(IMailService mailService,
            INotificationService notificationService)
        {
            _mailService = mailService;
            _notificationService = notificationService;
        }

        public void BackgroundJob()
        {
            var unhandledNotifications = _notificationService.GetUnhandledNotifications();

            foreach (var unhandledNotification in unhandledNotifications)
            {
                ...
                _mailService.SendAppointmentList(unhandledNotification);
            }
        }
}

MailService.cs

代码语言:javascript
复制
public class MailService : IMailService
{
        private readonly TattoogendaDbContext _context;
        private readonly IConfiguration _configuration;

        public MailService(TattoogendaDbContext context, IConfiguration configuration)
        {
            _context = context;
            _configuration = configuration;
        }
        
        ...

        public bool SendAppointmentList(NotificationLog notificationLog)
        {
            ...

            notificationLog.Destination = notificationLog.Project.Customer.Email;
            notificationLog.Title = notificationLog.Notification.Title;
            notificationLog.Body = body;
            notificationLog.HandledAt = DateTime.Now;
            notificationLog.Remarks = exception is null ? "": exception.Message;
            _context.SaveChanges(); // Exception is thrown here

            return result;
        }
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-04-10 20:39:02

我自己想出来的。

在NotificationService.cs中,我必须在LINQ中添加一个.ToList()

代码语言:javascript
复制
...
public IEnumerable<NotificationLog> GetUnhandledNotifications()
    {
        return _context.NotificationLogs.Where(l => l.HandledAt == null)
            .Include(l => l.Notification)
            .Include(l => l.Project)
            .ThenInclude(p => p.ProjectAppointments)
            .ThenInclude(a => a.ProjectAppointmentType)
            .Include(l => l.Project)
            .ThenInclude(p => p.Customer)
            .Include(l => l.Project)
            .ThenInclude(p => p.Artist)
            .ThenInclude(a => a.User)
            .Include(l => l.Shop)
            .ThenInclude(s => s.NotificationSettings).ToList();
    }
...
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71819357

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档