Quantcast
Channel: ASP.NET Core
Viewing all articles
Browse latest Browse all 9386

Entity framework not working like it should

$
0
0

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

  1. var companies2 = await _context.Companies.ToListAsync();


Viewing all articles
Browse latest Browse all 9386

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>