4

In startup.cs file, I add custom application context to the services container:

    public void ConfigureServices(IServiceCollection services)
    {
        var connection = @"...";                
        // Add Entity Framework services to the services container.
        services.AddEntityFramework()
            .AddSqlServer()
            .AddDbContext<ApplicationDbContext>(options =>
                options.UseSqlServer(Configuration["Data:DefaultConnection:ConnectionString"]))
            .AddDbContext<MyContext>(options => options.UseSqlServer(connection));

...

In the Configure method of this same class, I can get an instance using this command:

app.ApplicationServices.GetService(typeof(MyContext)) as MyContext;

It works, because I have the app variable there.

In other business class, how can I get an instance of MyContext?

Beetlejuice
  • 4,292
  • 10
  • 58
  • 84

0 Answers0