title: Instantiating DbContext within ViewModel VS passing controller's instance
body: <p>I have an editing ViewModel that does some DB work. </p>
  
  <p>Does it make any difference which of these approaches I take? </p>
  
  <ol>
  <li>Pass in the controller's DBContext instance to the ViewModel (example 1 below)</li>
  <li>Create a new DBContext instance within the ViewModel itself (example 2 below)</li>
  </ol>
  
  <p>If I pass in the controller's DBContext instance, that means <code>.Dispose()</code> will be called on it at some point (assuming there is the <code>.Dispose()</code> method in the controller). By creating the DBContext instance within the ViewModel method itself, this never happens.  Does that matter?</p>
  
  <hr />
  
  <p><strong>Example 1:</strong></p>
  
  <pre><code>public class ModelOne
  {
      public int ID { get; set; }
      public string PropA { get; set; }
      public string PropB { get; set; }
      public string PropC { get; set; }
      // etc...
  }
  
  
  public class EditModelOnePropAViewModel
  {
      public EditModelOnePropAViewModel(ApplicationDbContext db, int id)
      {
          ID = id;
          PropA = db.ModelOneDbSet
              .Where(i =&gt; i.ID == id)
              .Select(i =&gt; i.PropA)
              .FirstOrDefault();
      }
  
      public void SaveChanges(ApplicationDbContext db)
      {
          var modelOne = db.ModelOneDbSet.FirstOrDefault(i =&gt; i.ID == ID);
          modelOne.PropA = PropA;
          db.SaveChanges();
      }
  
      public string PropA { get; set; }
      public int ID { get; set; }
  }
  
  
  public class ControllerOne : Controller
  {
      private ApplicationDbContext DB = new ApplicationDbContext() { };
  
      [HttpGet]
      public ActionResult Edit(int id)
      {
          var viewModel = new EditModelOnePropAViewModel(DB, id);
          return View(viewModel);
      }
      [HttpPost]
      public ActionResult Edit(EditModelOnePropAViewModel postedModel)
      {
          if (ModelState.IsValid)
          {
              postedModel.SaveChanges(DB);
              return RedirectToAction("index");
          }
          return View(postedModel);
      }
  }
  </code></pre>
  
  <hr />
  
  <p><strong>Example 2:</strong></p>
  
  <pre><code>public class ModelTwo
  {
      public int ID { get; set; }
      public string PropA { get; set; }
      public string PropB { get; set; }
      public string PropC { get; set; }
      // etc...
  }
  
  
  public class EditModelTwoPropAViewModel
  {
      public EditModelTwoPropAViewModel(int id)
      {
          using (var db = new ApplicationDbContext())
          {
              ID = id;
              PropA = db.ModelTwoDbSet
                  .Where(i =&gt; i.ID == id)
                  .Select(i =&gt; i.PropA)
                  .FirstOrDefault();
          }
      }
  
      public void SaveChanges()
      {
          using (var db = new ApplicationDbContext())
          {
              var modelTwo = db.ModelTwoDbSet.FirstOrDefault(i =&gt; i.ID == ID);
              modelTwo.PropA = PropA;
              db.SaveChanges();
          }
  
      }
      public string PropA { get; set; }
      public int ID { get; set; }
  }
  
  public class ControllerTwo : Controller
  {
      [HttpGet]
      public ActionResult Edit(int id)
      {
          var viewModel = new EditModelTwoPropAViewModel(id);
          return View(viewModel);
      }
      [HttpPost]
      public ActionResult Edit(EditModelTwoPropAViewModel postedModel)
      {
          if (ModelState.IsValid)
          {
              postedModel.SaveChanges();
              return RedirectToAction("index");
          }
          return View(postedModel);
      }
  }
  </code></pre>