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

asp.net-web-api c# entity-framework


Entity Framework Core is being used in an ASP.Net Core 2.0 project I'm working on.

<PackageReference Include="Microsoft.EntityFrameworkCore" Version="2.0.1" />
  <PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="2.0.0" PrivateAssets="All" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="2.0.0"/>

I'm receiving the following problem in one of my list methods:

InvalidOperationException: A second operation started on this context before a previous operation completed. Any instance members are not guaranteed to be thread safe.

This is how I work:

    public ListResponseVM<ClientVM> GetClients([FromRoute] int currentPage, int pageSize, string search)
        var resp = new ListResponseVM<ClientVM>();
        var items = _context.Clients
            .Include(i => i.Contacts)
            .Include(i => i.Addresses)
            .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<ClientVM>(item);

            client.Addresses = new List<AddressVM>();
            foreach (var addr in item.Addresses)
                var address = _mapper.Map<AddressVM>(addr);
                address.CountryCode = addr.CountryId;

            client.Contacts = item.Contacts.Select(p => _mapper.Map<ContactVM>(p)).ToList();
            client.Urls = item.Urls.Select(p => _mapper.Map<ClientUrlVM>(p)).ToList();
            client.Objectives = item.Objectives.Select(p => _mapper.Map<ObjectiveVM>(p)).ToList();

        return resp;

It works when I run it locally, but when I deploy it to my staging server (IIS 8.5), it gives me this problem even though it was previously functioning fine. After I increased the maximum length of one of my models, the issue began to show up. I also changed the associated View Model's maximum length. Additionally, there are other additional list techniques that are effective and extremely comparable.

However, this task doesn't utilise the same object as the Hangfire job I had running. All I can come up with to be relevant is that. Any suggestions as to what may be causing this?

10/8/2018 12:40:06 PM

Popular Answer

I'm not sure whether you are resolving your DbContext everywhere it may be utilised utilising IoC and Dependency Injection. Make careful to register your DbContext as Transient if you use native IoC from.NET Core (or any other IoC-Container) and see this problem. Do




in place of


When using several threads, AddDbContext's scoped addition of the context might lead to issues.

When utilising async lambda expressions, async/await operations might also result in similar behaviour.

There are disadvantages to adding it as transitory. Because each class will have its own instance of your DbContext, you won't be able to modify an object across several classes that are utilising the context.

11/23/2018 7:11:45 AM

Related Questions


Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow