Pages

02 September, 2023

Entity Framework Core: A second operation started on this context before a previous operation completed

I'm working on a ASP.Net Core 2.0 project using Entity Framework Core And in one of my list methods I'm getting this error: InvalidOperationException: A second operation started on this context before a previous operation completed. Any instance members are not guaranteed to be thread safe. Microsoft.EntityFrameworkCore.Internal.ConcurrencyDetector.EnterCriticalSection() This is my method: [HttpGet("{currentPage}/{pageSize}/")] [HttpGet("{currentPage}/{pageSize}/{search}")] public ListResponseVM GetClients([FromRoute] int currentPage, int pageSize, string search) { var resp = new ListResponseVM(); var items = _context.Clients .Include(i => i.Contacts) .Include(i => i.Addresses) .Include("ClientObjectives.Objective") .Include(i => i.Urls) .Include(i => i.Users) .Where(p => string.IsNullOrEmpty(search) || p.CompanyName.Contains(search)) .OrderBy(p => p.CompanyName) .ToPagedList(pageSize, currentPage); resp.NumberOfPages = items.TotalPage; foreach (var item in items) { var client = _mapper.Map(item); client.Addresses = new List(); foreach (var addr in item.Addresses) { var address = _mapper.Map(addr); address.CountryCode = addr.CountryId; client.Addresses.Add(address); } client.Contacts = item.Contacts.Select(p => _mapper.Map(p)).ToList(); client.Urls = item.Urls.Select(p => _mapper.Map(p)).ToList(); client.Objectives = item.Objectives.Select(p => _mapper.Map(p)).ToList(); resp.Items.Add(client); } return resp; } I'm a bit lost especially because it works when I run it locally, but when I deploy to my staging server (IIS 8.5) it gets me this error and it was working normally. The error started to appear after I increase the max length of one of my models. I also updated the max length of the corresponding View Model. And there are many other list methods that are very similar and they are working. I had a Hangfire job running, but this job doesn't use the same entity. That's all I can think to be relevant. Any ideas of what could be causing this?

No comments:

Post a Comment

Thanks