Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
394 views
in Technique[技术] by (71.8m points)

Blazor - how to instantiate a dbContext in code

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...