How to access database context outside of razor page?

I built a small web-app for college last semester and ran into an issue that I wasn’t able to properly solve. The app is a price tracker that’s using Selenium or curl to scrape products from webpages and track their price over time.
My issue is, how do I properly access the database context outside of the .cshtml.cs files? My current solution to this was to have a global variable that is assigned to the database context from the Main() class as such: