Coming from a Java environment I'm having a few issues wrapping my head about the inheritable DBContext class. with Java EE I used to:
- Set up one or multiple DB Contexts (if separate clusters of entities where affected);
- Added the Context to respective DataAccessor classes that handled the query execution via DI;
Now with EF Core practically all samples I see create an instance of a MyDBContext by calling the default constructor:
using (var db = new myContext()){...}
This raises a few questions for me:
- With this Method each DataAccessor class that calls the constructor has its own instance of the Context. Wouldn't it be nicer to only ave one and use DI to inject it when needed?
- How do I call the constructor if i didn't overload
OnConfiguring(...)
to pass the options, but instead usedAddDbContext
as a Service inStartup.cs
? Now the overloaded constructor with the options expects them to be passed on each time the constructor is called. - Is having multiple DBContexts per application/Db even a good practise with EF Core?