Access _context in an ASP.NET Core MVC Model

Viewed 5195

I'm trying to interact with the database from within a Model in an ASP.NET Core 1.0 MVC project. Scaffolding has been used to create Controllers and views from the Models.

Ideally I would like to access it like I would in a controller with the _context but I failed finding a way to do so.

I also tried to create a new connection from the model:

using (var context = new ApplicationDbContext()){
    // Code here
}

But the ApplicationDbContext constructor requires options arguments like the default connection string which I failed retrieving from the config.

Public ApplicationDbContext(DbContextOptions options) : base(options) {
}

My guess is that I misunderstand something very basic because this should be easy to do.

Any thoughts?

1 Answers
Related