error message : System.InvalidOperationException: 'A second operation was started on this context instance before a previous operation completed. This is usually caused by different threads concurrently using the same instance of DbContext. For more information on how to avoid threading issues with DbContext
It occur error when I get data from db at second time.
_context.UserPTO.Where(up => up.UserId == user.Id).FirstOrDefault()
in below source
How can I do for this?
private DbContextOptions<DataContext> GetAllOptions()
{
var optionBuilder = new DbContextOptionsBuilder<DataContext>();
optionBuilder.UseSqlServer(AppSettings.ConnectionString);
return optionBuilder.Options;
}
public void UpdatePTO()
{
try
{
using (_context = new DataContext(GetAllOptions()))
{
var users = _context.Users.ToList();
if (users != null && users.Count > 0)
{
foreach (var user in users)
{
var plusPTO = 0.00M;
var workedYear = DateTime.Now.Date.Year - user.StartWorkDate.Date.Year;
switch (workedYear)
{
case int n when (3 < n):
plusPTO = 1.25M;
break;
case int n when (3 < n):
plusPTO = 1.50M;
break;
};
var userPTO = _context.UserPTO.Where(up => up.UserId == user.Id).FirstOrDefault();
if (userPTO != null)
{
userPTO.Pto = userPTO.Pto + plusPTO;
}
var userPTOHistory = new UserPTOHistory
{
UserId = user.Id,
PTOType = "",
UserPTOId = 0,
CurrentPTO = userPTO.Pto,
NeedPTO = 0,
VerifiedType = "Accepted",
CreatedDate = DateTime.Now,
VerifiedDate = DateTime.Now,
CalculatedPTO = userPTO.Pto
};
_context.Add(userPTOHistory);
_context.SaveChangesAsync();
}
}
}
}
catch (Exception)
{
throw;
}
}
// program.cs ( cf) this is a worker service project. )
using Microsoft.EntityFrameworkCore;
using WorkerService;
using WorkerService.Services;
IHost host = Host.CreateDefaultBuilder(args)
.ConfigureServices((hostContext, services) =>
{
IConfiguration configuration = hostContext.Configuration;
AppSettings.Configuration = configuration;
AppSettings.ConnectionString = configuration.GetConnectionString("DefaultConnection");
var optionBuilder = new DbContextOptionsBuilder<DataContext>();
optionBuilder.UseSqlServer(AppSettings.ConnectionString);
services.AddScoped<DataContext>(d => new DataContext(optionBuilder.Options));
services.AddHostedService<Worker>();
})
.Build();
await host.RunAsync();