Does the using {}
block work in the same way in both web and desktop applications in the sense that when we use it in controller
? It is a good practice to declare it in actions that there is contact with database
Follow examples, which one would be a good practice?
public class UsuarioController : Controller
{
private EntidadesDCSystem db = new EntidadesDCSystem();
[HttpPost]
public ActionResult Adicionar(Usuario usuario)
{
if (ModelState.IsValid)
{
db.Usuario.Add(usuario);
db.SaveChanges();
return RedirectToAction("Index");
}
return View(usuario);
}
Or:
public class UsuarioController : Controller
{
[HttpPost]
public ActionResult Adicionar(Usuario usuario)
{
using (EntidadesDCSystem db = new EntidadesDCSystem())
{
if (ModelState.IsValid)
{
db.Usuario.Add(usuario);
db.SaveChanges();
return RedirectToAction("Index");
}
return View(usuario);
}
}