Accessing DbContext Class from Asp Mvc Areas

763 views Asked by At

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?

1

There are 1 answers

4
Ali Soltani On BEST ANSWER

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