Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I access Configuration in DbContext class?

I'm using ASP.NET Core 2 I'm trying to access Configuration in context class,I've done configurations like this: in Program.cs:

 public static IWebHost BuildWebHost(string[] args) =>
            WebHost.CreateDefaultBuilder(args)
        ......
             .ConfigureAppConfiguration((builderContext, config) =>
             {
                 IHostingEnvironment env = builderContext.HostingEnvironment;

                 config.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
                       .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true, reloadOnChange: true);
             })

           .Build();

in Startup.cs:

 public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
        }

I can access it in controllers like this:

public HomeController(IConfiguration configuration)
   {...}

but how do I access it in context class? thanks

like image 361
mrapi Avatar asked Nov 06 '17 06:11

mrapi


People also ask

What does DbContext class do?

Definition. A DbContext instance represents a combination of the Unit Of Work and Repository patterns such that it can be used to query from a database and group together changes that will then be written back to the store as a unit.

What is meant by Entity Framework Core DB configuration?

Entity Framework Core uses a set of conventions to build a model based on the shape of your entity classes. You can specify additional configuration to supplement and/or override what was discovered by convention.

Is DbContext scoped or transient?

This example registers a DbContext subclass called ApplicationDbContext as a scoped service in the ASP.NET Core application service provider (a.k.a. the dependency injection container). The context is configured to use the SQL Server database provider and will read the connection string from ASP.NET Core configuration.

How to add iconfiguration to a dbcontext?

In your situation where you need IConfiguration in a DbContext class, you will have to add it to the constructor: public class MyDbContext : DbContext { private readonly IConfiguration _configuration; public MyDbContext (IConfiguration configuration) { _configuration = configuration; } } But you also have to get MyDbContext from DI then.

What is dbcontext in Entity Framework?

The class that derives DbContext is called context class in entity framework. DbContext is an important class in Entity Framework API. It is a bridge between your domain or entity classes and the database. DbContext is the primary class that is responsible for interacting with the database. It is responsible for the following activities:

How to add dbcontext type to the service container?

Add the DbContext type to the service container by using the AddDbContext method with Scoped lifetime (Recommended ). Using AddDbContext(..) ensure that you are registering the DBCOntext as a scoped object. i.e every new request will use the services injected and hence the new DBContext. 1. 2.

How to get the dbcontext of the model in the controller?

In the controller (or anywhere really) I call public HomeController (IConfiguration configuration) => _modelContext = configuration.Get<ModelContext> (); which throws the unexpected exception. No database provider has been configured for this DbContext.


1 Answers

In your situation where you need IConfiguration in a DbContext class, you will have to add it to the constructor:

public class MyDbContext : DbContext
{
    private readonly IConfiguration _configuration;

    public MyDbContext(IConfiguration configuration)
    {
        _configuration = configuration;
    }
}

But you also have to get MyDbContext from DI then. So a static class cannot receive it as a parameter. You will have to re-think how you are using the DbContext, and change it so that you get the context somewhere with access to DI, and pass that as an argument to any static methods that require it.

like image 89
juunas Avatar answered Oct 11 '22 23:10

juunas