Read documentation on configuring DbContext: https://docs.microsoft.com/en-us/ef/core/miscellaneous/configuring-dbcontext
Basically, you add it to your services:
public void ConfigureServices(IServiceCollection services)
{
services.AddDbContext<BloggingContext>(options => options.UseSqlite("Data Source=blog.db"));
}
Then you inject it into whatever class you want. An easy example would be inejecting it into a Controller (but you could inject into any class that is added to your services):
public class MyController
{
private readonly BloggingContext _context;
public MyController(BloggingContext context)
{
_context = context;
}
...
}
The Dependency Injection library will then handle disposal - you do not call Dispose directly. This is described in documentation here.
The framework takes on the responsibility of creating an instance of
the dependency and disposing of it when it's no longer needed.