Tell me how to work correctly with the database context (ef core) inside Jobz Quartz.net

To work with the database I want to create _context as a Joba field It would look like this:

 public class LoaderJob:IJob { private PsgrContext _context; public Task Execute(IJobExecutionContext context) { return Task.Run(() => { var q = _context.Photos(); //Тут уже работать с контекстом Console.WriteLine("Execute"); }); } public LoaderJob() { _context = new PsgrContext(); Console.WriteLine("Ctor Test"); } } 

But unfortunately, this code:

 NameValueCollection props = new NameValueCollection { {"quartz.serializer.type", "binary"} }; StdSchedulerFactory factory = new StdSchedulerFactory(props); IScheduler scheduler = await factory.GetScheduler(); // and start it off await scheduler.Start(); // define the job and tie it to our HelloJob class IJobDetail job1 = JobBuilder.Create<LoaderJob>() .WithIdentity("job1", "group1") .Build(); // Trigger the job to run now, and then repeat every 10 seconds ITrigger trigger1 = TriggerBuilder.Create() .WithIdentity("trigger1", "group1") .StartNow() .WithSimpleSchedule(x => x .WithIntervalInSeconds(1) .RepeatForever()) .Build(); // Tell quartz to schedule the job using our trigger var jobs = new Dictionary<IJobDetail, IReadOnlyCollection<ITrigger>>(); jobs[job1] = new [] {trigger1}; await scheduler.ScheduleJobs(jobs, true); 

Will return the following conclusion:

 Ctor Test Execute Ctor Test Execute Ctor Test Execute Ctor Test Execute Ctor Test Execute Ctor Test Execute 

I can pass _context as joba parameters, or use static fields. But this is not thread safe.

And since Working with the database from different contexts is thread-safe; the best solution would be to initialize the context inside the job. But how to do it, I do not know.

    0