In MVC 4 I ended up overriding the controller factory to allow my ApplicationDbContext
to be injected into each controller as it was instantiated. I was sure that throughout my requests I would never have two references to my ApplicationDbContext
which ensured I didn't have any multithreading issues.
Now that MVC 5 is here, what's the proper way of implementing the repository pattern with OWIN? I've heard with OWIN that it creates a new UserManager / ApplicationDbContext per request. If this is true, then I would like to use the database context in my controllers / repositories. Is it possible to pass the database context into a controller? (Something to the lines of HttpResponse.GetOwinContext.Environment["owin.DatabaseContext"]
??)
There seems to be a half-finished question here that doesn't have an answer to the question: OWIN DbContext and a single DbContext for all of my Repositories. (also one here: Owin Context to use a single instance per request) Any guidance is appreciated.