c# - ASP.NET MVC Entity Framework DB access in Controller -
i trying access db created display list. doing in controller. red underlining db part. saying "the name db not exist in current context." missing thing connect db in code?
using system.web; using system.web.mvc; using system.io; using system.web.helpers; using mis424assignments.models; namespace mis424assignments.controllers { [authorize (users="admin@wwu.edu")] public class retailcontroller : controller { // get: retail [allowanonymous] public actionresult index() { string sql = "select * product order newid()"; list<product> productlist = db.product.sqlquery(sql).tolist(); return view(); } } }
try
[authorize (users="admin@wwu.edu")] public class retailcontroller : controller { private readonly retailstoreentities1 db; public retailcontroller() { db = new retailstoreentities1(); } // get: retail [allowanonymous] public actionresult index() { string sql = "select * product order newid()"; list<product> productlist = db.product.sqlquery(sql).tolist(); return view(); } }
Comments
Post a Comment