I have created a site with MVC5/ EF6 and am using Ninject as the DI container to bind it together.
All is working well except on one page, where i am making 2 ajax calls at the same time.
The controller/repository spits out various errors such as:
"A first chance exception of type 'System.Data.Entity.Core.EntityException' occurred in mscorlib.dll. Additional information: The underlying provider failed on Open."
One call to the api will always work, the second works intermittently. If i create the datacontext directly in the controller, it all works fine, so the error must relate to Ninject, but i can't work out what i need to do to sort it. Varying the scope to Transient/Thread just changes the error.
Any help much appreciated.
Here is my (edited) code:
Called from Global.asax Application_Start()
var context = new myDataContext();
// Lowest down wins
IKernel kernel = new StandardKernel();
DependencyResolver.SetResolver(new NinjectDependencyResolver(kernel));
GlobalConfiguration.Configuration.DependencyResolver
= new App_Start.NinjectResolver(kernel);
kernel.Bind<IRepository<Models.MyClass>>()
.To<MyRepository>()
//.InTransientScope()
//.InThreadScope()
.InRequestScope()
.WithConstructorArgument("context", context);
Api Controller
public class MyApiController : ApiController
{
IRepository<MyRepository> _repository;
public EMAAFController(IRepository<MyRepository> repository)
{
_repository = repository;
}
public IEnumerable<Models.MyClass> Get(int id)
{
var itemToReturn = _repository.GetAll().Where(i => i.id == id).ToList();
return itemToReturn;
}
}