So i'm following a tutorial about the entity framework in ASP.net core to get data from the database, but if i do the same thing like the guy in the video, it won't work. I got a workaround, but still wanna know why it isn't working.
The way like it should in the tutorial: (https://www.youtube.com/watch?v=jp-0h0wFjhw&t=1145s)
ets1Context _context; public CompanyController(ets1Context context) { _context = context;; } public async Task<ActionResult> Index() { var companies2 = await _context.Companies.ToListAsync(); return View(companies2); }
The error that i get:
InvalidOperationException: Unable to resolve service for type 'cms.Entity.ets1Context' while attempting to activate 'cms.Controllers.CompanyController'.
So my workaround is:
public readonly ets1Context context; ets1Context _context; public CompanyController() { _context = context; } public async Task<ActionResult> Index() { var companies2 = await _context.Companies.ToListAsync(); return View(companies2); }
But now i don't get the same error, but i'm still not getting any data to my variable. is there someone who know how to solve this problem and knows why the first method doesn't work?
The error that i now get is
NullReferenceException: Object reference not set to an instance of an object.
MoveNext in CompanyController.cs
, line 30
- var companies2 = await _context.Companies.ToListAsync();