I get this error when one API call in my EF Core C# web API is hit rapidly.
Proj> System.InvalidOperationException: This SqlTransaction has completed; it is no longer usable.
Proj> at System.Data.SqlClient.SqlTransaction.ZombieCheck()
It seems like the solution is to:
- Dispose of a connection using a
using
statement c#corner link - Change
AddDbContext
toAddDbContextPool
based on this SO post
How does a using
statement for a readonly context help prevent the error below? It seems counter intuitive not to call new MyContext()
public class MyController : Controller
{
private readonly MyContext _mc;
public GreenCardController(MyContext mc){_mc=mc;}
[HttpGet("GetCompanies")]
public IEnumerable<vwCompany> GetCompanies(int lgid)
{
using (MeSeeksContext mc = _mc){
return mc.myTable.Where(x=>x.id==lgid)
}
}