它是正确的控制器申报“的DbContext”的全局变量,然后将其用于所有数据库操作?
例:
public class ProductController : Controller
{
private readonly DBContextEntities _db = new DBContextEntities();
public ActionResult Index()
{
var products = _db.Products.ToList();
return View(products);
}
public ActionResult Create()
{
_db.Products.AddObject(new Product{Name="x",Price="5.2"});
_db.SaveChanges();
return View(products);
}
}
请指教,