I have a question on the write syntx to implement the Dispose inside my asp.net mvc-5 web application.

now i created a new asp.net mvc-5 web application using Visual Studio 2013 , and i mapped my database tables inside an edmx file. then i created a new Controller class. now the default code for Dispose() is as follow at the end of the controller class:-

public class DeptsController : Controller
    {
  private ApplicationDbContext db = new ApplicationDbContext();

  //code goes here..

  protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                db.Dispose();
            }
            base.Dispose(disposing);
        }

but at the same time i find many online articles and books which use the following syntax to Dispose the current request as in this link:-

protected virtual void Dispose(bool disposing)
        {
            if (!this.disposed)
            {
                if (disposing)
                {
                    context.Dispose();
                }
            }
            this.disposed = true;
        }

        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }

so i have this question :- which syntax for dispose i should/prefer to use ? and do these 2 approaches generate the same result ?

Related posts

Recent Viewed