title: Where to use db context for viewmodels in MVC
body: <p>The Controller should not directly use the DB context, and the respository should not know about view models (representation). </p>
  
  <p>The underlying issue is of course to avoid selecting all columns from the DB, only those that are needed.</p>
  
  <p>How is this problem normally solved?</p>
  
  <p>Example 1: DB context in controller: </p>
  
  <pre><code>public ActionResult Products()
  {         
     var model = from p in _context.Products 
     select new ProductItemViewModel
     {
             ProductName = p.ProductName
     };
     return View(model);        
  }
  </code></pre>
  
  <p>Example 2: ViewModel in repository:</p>
  
  <pre><code>public ProductItemViewModel Products()
  {         
    return  from p in _context.Products 
      select new ProductItemViewModel
      {
         ProductName = p.ProductName
      };
  }
  </code></pre>