0

I am interested in programming as an amateur. I am trying to make MVC based web program using c#. I have 2 scheduled jobs. I am using quartz.net for it. One of them does a long-running job in the background, while the other sends notification messages to the user according to the status of the job. The first job will run every 5 minutes while the other (the notification one) will run 1 every minute. But if the other job starts running while the long running job is running in the background, I get the following exception:'System.Data.Entity.Core.EntityException' in EntityFramework.SqlServer.dll

I am using one scheduler for 2 jobs.

            await scheduler.ScheduleJob(notificationTaskJob, notificationTaskTrigger);
            await scheduler.ScheduleJob(programTaskJob, programTrigger);

and i am using Singleton Design Pattern to create DatabaseContext. I'm not sure if it has anything to do with it.

I hope I explained my question properly.

user2864740
  • 60,010
  • 15
  • 145
  • 220
  • 1
    Each thread/task should use *it’s own context* and not share it. If the “singleton” was a “context creator/factory” and not a “context”, this issue would not exist. – user2864740 Jan 09 '22 at 08:26
  • https://stackoverflow.com/a/54025622/2864740 , https://stackoverflow.com/a/6126693/2864740 , https://learn.microsoft.com/en-us/ef/core/dbcontext-configuration/ “**DbContext is not thread-safe. Do not share contexts between threads**. Make sure to await all async calls before continuing to use the context instance.” – user2864740 Jan 09 '22 at 08:29
  • 1
    thanks. I got it. – ibrahim avci Jan 10 '22 at 13:01

0 Answers0