DEV Community

Discussion on: .Net Core Shared Link Returning Null?

Collapse
 
mitchelln11 profile image
mitchelln11

So I did get it working for anybody else that runs across this.

Previously, the following would work in .NET Framework: (On the controller - HikerController in my case)

private ApplicationDbContext _context = new ApplicationDbContext();

Now in .NET Core, you need a constructor; starts a similar way:

private ApplicationDbContext _context;
public HikerController(ApplicationDbContext context)
{
  _context = context
}

Then you would reference database info something like this: _context.Hikers....
I have the following to display all of my hikers:

        public ActionResult Index()
        {
            return View(_context.Hikers.ToList());
        }

*If you don't use a constructor and pass in the ApplicationDbContext, you will get the following:

An unhandled exception occurred while processing the request.
InvalidOperationException: 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<TContext> object in its constructor and passes it to the base constructor for DbContext.
Microsoft.EntityFrameworkCore.Internal.DbContextServices.Initialize(IServiceProvider scopedProvider, IDbContextOptions contextOptions, DbContext context)