I'm using EF-Core in a web-api project, and using the DI mechanism to inject the DBContext to action methods. BUT - In addition - I would like to reach the DB using the EF dbContext, when the server is up, in the startup method - this is a non http context, meaning - the DBContext is not initiated yet - it is initiated in the ConfigureServices method, and it is initiate after the stratup method. For further explanation, this is the Startup.cs and the rest of the relevant flow:
public class Startup { public IConfiguration Configuration {get; } public Startup(IConfiguration configuration) { Configuration = configuration; RepositoriesManager.Initiate(configuration); //In addition - I now would like to use it to initiate repository by using the EF Core dbContext //Another option is to run this methos after adding the dbContext serice - in the ConfigureServices } public void ConfigureServices(IServiceCollection services) { services.Add.... services.AddDbContext<MyDBContext>(options => options.UseSqlServer(Configuration.GetConnectionString("myDB))); //Option to execute it here instead of in the Startup RepositoriesManager.Initiate(configuration); } } RepositoriesManager is a static class that using the configuration to get data from external API
public static Class RepositoriesManager { static RepositoriesManager(){} //static constructor public static void Initiate(IConfiguration configuration) { //to make a long story short: GetDataFromDBUsingEF(); } //This method can be called also from action method (controller) - where the myDBContext is passed - this is not the case here public static GetDataFromDBUsingEF(MyDBContext myDBContext = null) { if (myDBContext == null) { //one option - not working: var serviceCollection = new Microsoft.Extensions.DependencyInjection.ServiceCollection(); var sp = serviceCollection.GetService<MyDBContext>(); //second option - also not working: myDBContext = new MyDBContext(); // This should initiate the DBContext in some scenarios } } } I'm trying to use the dbContext OnConfiguration method:
protected override void OnConfiguration(DbContextOptionsBuilder optionsBuilder) { if (!optionsBuilder.IsConfigured) { IConfigurationRoot configuration = new ConfigurationBuilder().SetBasePath(AppDomain.CurrentDomain.BaseDirectory).AddJsonFile("appsettings.json").Build(); optionsBuilder.UseSqqlServer(configuration.GetConnectionString("<connection string key>")); } } This method should be called in every DbContext initiation using its constructor. Right now it is not being reached when I initiate the dbContext. I assume that the reason is what the documentation claims: "If a model is explicitly set on the options for this context (via Microsoft.EntityFrameworkCore.DbContextOptionsBuilder.UseModel(Microsoft.EntityFrameworkCore.Metadata.IModel)) then this method will not be run".
How can I get the DBContext when its not injected ?
Startup"method".Configuremethod and use it as needed. Read up on how you should be using Startup here learn.microsoft.com/en-us/aspnet/core/fundamentals/startup