I'm creating an ASP.NET Core application. It is using Entity Framework Core for database access. I'm using services.AddDbContext in Startup.cs and the DB Context is injected into my controller as expected.
I am also have a background task using IHostedService that is added as a Singleton. I'd like to have an instance of my DBContext in my implementation of IHostedService. When I try to do this I get a run time error that my IHostedService cannot consume a scoped service (my DB Context).
The DB Context class takes a parameter of DbContextOptions options and passes the options to the base constructor (DbContext).
I need to create an instance of my DB Context in my implementation of IHostedService (a singleton object) but I can't seem to figure out how to correctly create a new instance of DbContextOptions from my IHostedService implementation.