Getting DbContext via Configuration with DbContextOptions c# entity-framework-core


I am trying to get the DbContext I registered with options via services.AddDbContext(...) on the service provider of the project, but when calling configuration.Get<ModelContext> it can not be constructed as the options apparently weren't provided and therefore also no database provider is given.

I am using ASP.NET Core 2.2 with Entity Framework Core 2.2.3 and my DbContext is defined in a separate project.

My DbContext:

public class ModelContext : DbContext
    public ModelContext(DbContextOptions<ModelContext> options) : base(options) { }

    public ModelContext() { }

I did not override OnConfiguring(DbContextOptionsBuilder) in ModelContext.

public class StartUp
    public void ConfigureServices(IServiceCollection services)
        public services.AddEntityFrameworkSqlServer();
        services.AddDbContext<ModelContext>(options => options.UseSqlServer(modelConnectionString));

In the controller (or anywhere really) I call public HomeController(IConfiguration configuration) => _modelContext = configuration.Get<ModelContext>(); which throws the unexpected exception.

What I specifically get is an InvalidOperationException with the message:

No database provider has been configured for this DbContext. A provider can be configured by overriding the DbContext.OnConfiguring method or by using AddDbContext on the application service provider. If AddDbContext is used, then also ensure that your DbContext type accepts a DbContextOptions object in its constructor and passes it to the base constructor for DbContext.

According to the documentation I read and examples I looked at, the ModelContext should be created with the options I defined when calling AddDbContext<ModelContext>. Is the Get method the wrong one to use?

4/1/2019 8:45:00 AM

Accepted Answer

After configuring the db context service in "ConfigureServices" method of the Startup.cs file with something like this :

var connectionString = Configuration.GetConnectionString("DefaultConnection");
services.AddDbContext<BottinContext>(options => options.UseSqlServer(connectionString)) ;

Simply add a :

ModelContext db

parameter to the constructor of your controller and let DI magic happen.

If you've got many controllers and wish to simplify things, you can use a base contructor that holds the db context

  public BaseController(ModelContext context /* as well as other injections */)
      _db = context;
    internal ModelContext _db;
4/1/2019 12:24:06 AM

Popular Answer

you are trying to get dbContxt instance in a wrong way. Get method is not used to get instance of dbContext object that you registered with dependency injection container. if you want to get instance of your dbContext class that you registered you can inject it through construction injection for example

public class RepositoryWrapper : IRepositoryWrapper
        private readonly ModelContext _modelContext;

        public RepositoryWrapper(ModelContext modelContext)
        _modelContext= modelContext;

is something i am doing in my project.

Related Questions


Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow