Removing context file content from dbcontext in asp.net mvc

     public class AccountController : Controller
     {

        private readonly mydbcontext db = new mydbcontext();

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


        public ActionResult Login()
            {
               var result = db.User.Select(x=>x);
// Do i need to call db.Dispose here? or will it get dispose automatically?
                return View(result );
            }              
      }

      

I am using the mydbcontext db object here to get a list of users.
I need to explicitly call db.dispose, or use a statement inside my login action, or will it automatically dispose of the mydbcontext object.

+3


source to share


1 answer


db

will be removed when AuctionController

removed. You don't need to do more.



But the best solution is to use DependancyInjection. Like this

-1


source







All Articles