Does anyone know how to instantiate a DbContext in code in a Blazor app. When I try and instantiate my context directly it requires a DBContextOptions which I don't seem to be able to find how to create (Startup.cs uses a IServiceCollection which I don't have access to in back-end code).
To get around this, I am having to pass in the DB context from the Blazor components which I would like to avoid as I have other non-component code that also needs to access the same data entities.
I am using Blazer, Server app with .Net 5 and currently do something like this.
@inject MyDbContext _context;
OnInitializedAsync() { myService.SomeMethod(_context,...) }
What I would like to do is the following.
OnIntializeAsync() { myService.SomeMethod(...) }
SomeMethod(...) {
MyDBContext _context = new MyDBContext(some parameters); // preferably in a using clause.
_context.GetSomeDataMethod();
}
question from:
https://stackoverflow.com/questions/65910187/blazor-how-to-instantiate-a-dbcontext-in-code 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…