Search code examples
c#asp.net-mvcentity-frameworkef-code-firstareas

Accessing DbContext Class from Asp Mvc Areas


I have an admin area in my ASP.Net MVC project. I want to perform some CRUD operations in that admin area. For that I have to make some model classes. I have completed creating action methods and views.

Should I make the model classes in area model folder or root model folder? I have a dbcontext class for my code first approach in root folder. how can I make object of that dbcontext in admin area home controller?

How to access model classes from area to root controller and vice versa?


Solution

  • You can access to dbcontext (EF_Sample) in controller (HomeController) like this:

    namespace Area_Model.Areas.admin.Controllers
    {
        public class HomeController : Controller
        {
            //
            // GET: /admin/Home/
            public ActionResult Index()
            {
                using (EF_Sample db = new EF_Sample())
                {
                    var sampleList = db.Students.ToList();
                }
                return View();
            }
        }
    }
    

    access dbcontext