Hello Team ,
I using Entity framework and facing issue in DBcontext when second thread hit same method.
Could you please provide sample code how to create new dbcontext for each thread.
My code something like this
internal class DataAccess : IDataAccess
{
private readonly AppDbContext _context;
internal DataAccess(int userID, AppDbContext dbContext)
{
UserID = userID;
_context = dbContext;
}
method1()
{
_context calling
}
method2()
{
_context calling
}
}
internal class Employee
{
private readonly DataAccess dbaccess;
internal Employee(int userID)
{
UserID = userID;
DataAccess dbaccess = new DataAccess()//_setting DbContext here thru constructor ;
}
methodTest()
{
System.Threading.Tasks.Task.Factory.StartNew(() =>
{
dbaccess.method1() /// getting error for second thread and nth thread
});
}
}