I need to access the current db context in my OnActionFilterExecuting method which receives ActionExecutingContext parameter (filterContext).
I can see it in my local stack while debugging but I don’t know how to access it.
I have tried:
filterContext.Controller.dbfilterContext.Controller(ftvcdl.Controllers.PartController).db
Here is my debug trace: https://i.stack.imgur.com/5CsB1.png
PS: I am new to C# so I don’t fully understand the syntax or how to read the debug stack very well, although I am learning!
You will need to cast the controller to something that provides access to the DB, for example:
However, you will get issues if you use the attribute on something that isn’t the
PartController(or a method in there). You could perhaps improve that by using a base-type or interface instead ofPartController.Another approach would be to use the http-context to convey the DB:
and stuff the db into the http-context when you create it (and equally, make sure to dispose it when the request ends)
Finally, you could just have a separate DB-context in the attribute; connection pooling means this may not be a problem: