In a controller, is there any difference between declaring a database context instance like this:
public class MyController : Controller
{
private readonly MyDatabaseEntities _context = new MyDatabaseEntities();
public ActionResult Index()
{
...
}
}
Versus this:
public class MyController : Controller
{
private readonly MyDatabaseEntities _context;
public MyController()
{
_context = new MyDatabaseEntities();
}
public ActionResult Index()
{
...
}
}
I'm just beginning to realise that the way I've been handling database context in my ASP.NET apps may not have been correct (or at least ideal) for a while and I'm reading up on it and thinking of how I'm going to start using Dependency Injection, but for starters I was just wondering if I'm even instantiating database contexts in my controllers correctly.
Incidentally, I'm still working in .NET Framework 4.7.2. Hopefully we'll start working in .NET 5 soon!