0

I encounter many trouble with this error in an ASP Net CORE mvc project and I didn't find a way to solve it :

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

It append on a non async function :

public Employee GetEmployeeByuserID(String userID)
    {
         return _applicationDBContext.Employee.Where(e => e.UserId.Equals(userID)).First();
    }

I've try to make this function async but it just make the error append somewhere else.

The function is in a DAL with a dependency injection of _applicationDBContext. The Dal is declared is declared in AddTransient in the startup.cs I've seen some solution with an await but I counld'n find where I'm suppose to put it.

EDIT

If I made a breakpoint to pause the code before return _applicationDBContext.Employee.Where(e => e.UserId.Equals(userID)).First(); and continue, it work well.

Alexis
  • 401
  • 1
  • 8
  • 20
  • Possible duplicate: https://stackoverflow.com/questions/48767910/entity-framework-core-a-second-operation-started-on-this-context-before-a-previ –  May 23 '18 at 18:15
  • Possible duplicate of [Entity Framework Core: A second operation started on this context before a previous operation completed](https://stackoverflow.com/questions/48767910/entity-framework-core-a-second-operation-started-on-this-context-before-a-previ) – CodeNotFound May 23 '18 at 18:29
  • I've already tried what these answers suggest, the dependency injection is a private readOnly, I maybe calling GetEmployeeById somewhere else, I will check tomorrow at work, what's the solution if I need to call it multiple time during the execution ? – Alexis May 23 '18 at 19:34

1 Answers1

0

I finally made it work by creating a new DAL each request :

Employee emp = new EmployeeDal(new ApplicationDbContext()).GetEmployeeByuserID(appUser.Id);

If you have a better solution, please let me know.

Alexis
  • 401
  • 1
  • 8
  • 20