1.3.6.Query objects

  • 1.Modify model: add ApplicationDbContext

        private ApplicationDbContext _context;
        public CustomersController()
        {
            _context = new ApplicationDbContext();
        }
        protected override void Dispose(bool disposing)
        {
            _context.Dispose();
        }
  • 2.Use ApplicationDbContext

        public ActionResult Index()
        {
            var customers = _context.Customers.ToList();
            return View(customers);
        }
        [Route("customers/detail/{index}")]
        public ActionResult Detail(int index)
        {
            var customer = _context.Customers.SingleOrDefault(c => c.Id == index);
            if (customer == null)
            {
                return HttpNotFound();
            }
            return View(customer);
        }

Last updated