How to instantiate a DbContext in EF Core

.net-core c# entity-framework-core


I have setup .net core project and db context also. But i cant start using dbContext yet due this error-

"there is no argument given that corresponds to the required formal parameter 'options'"


public IActionResult Index()
    using (var db = new BlexzWebDb())

    return View();

Dbcontext Code:

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

    public DbSet<User> Users { get; set; }
    public DbSet<Role> Roles { get; set; }
    public DbSet<AssignedRole> AssignedRoles { get; set; }


error picture attached. Whats the possible fix for that issue? Thanks in advance


9/19/2018 4:33:17 PM

Accepted Answer


These days the prefered method to add a DbContext to the service collection is to use the AddDbContextPool method:

//hey, options!
//hey, DbContextPool
services.AddDbContextPool<BlexzWebDb>(options => 


for more info, see msdn

In EF Core it's common to pass some DbContextOptions to the constructor.

So in general, a constructor looks like this:

public BlexzWebDb(DbContextOptions<BlexzWebDb> options) : base(options)

As you can see there, there is no valid overload in the form of a parameter-less constructor:

Thus, this does not work:

using (var db = new BlexzWebDb())


.Net Core has IoC implemented in it's roots. Okay, this means; you don't create a context, you ask the framework to give you one, based on some rules you defined before.

Example: somewhere you will register your dbcontext, (Startup.cs):

//typical configuration part of .net core
public void ConfigureServices(IServiceCollection services)
    //some mvc 

    //hey, options! 
    services.AddDbContextPool<BlexzWebDb>(options => 

Now the registering part is done, you can retrieve your context from the framework. E.g.: inversion of control through a constructor in your controller:

public class SomeController : Controller
    private readonly BlexzWebDb _db;

    //the framework handles this
    public SomeController(BlexzWebDb db)
        _db = db;



So, why not just provide the arguments and new it?

Obviously that will work as well. But, Inversion Of Control is considered to be a good practice. When doing asp dotnet core you should use it quite often because most libraries provide extension methods to use it.

Therefore, instead of providing "just a way to instantiate" the object, I'll try to get you onto the right track - inline with the framework. It will save you some hassle afterwards. Besides, otherwise "use an activator's CreateInstance" would just be as valid as an answer ;-)

Some links:

2/21/2020 12:05:35 PM

Popular Answer

Instantiate new object of DbContext from ConnectionString

var connectionstring = "Connection string";

var optionsBuilder = new DbContextOptionsBuilder<ApplicationDbContext>();

ApplicationDbContext dbContext = new ApplicationDbContext(optionsBuilder.Options);

// Or you can also instantiate inside using

using(ApplicationDbContext dbContext = new ApplicationDbContext(optionsBuilder.Options))
   // stuff

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