title: Where does an MVC Controller get it's database context at run time
body: <p>Given this code:</p>
  
  <pre><code>namespace Eisk.Controllers
  {
      public class EmployeesController : Controller
      {
          DatabaseContext _dbContext;
  
          public EmployeesController(DatabaseContext databaseContext)
          {
              _dbContext = databaseContext;
          }
  
          public ViewResult Index()
          {
              var employees = _dbContext.EmployeeRepository;
  
              return View(employees.ToArray());
          }
  </code></pre>
  
  <p>Note that the constructor doesn't new up a database.</p>
  
  <p>When accessed from a unit test I can inject a databaseContext and the controller will use that for the duration of the test. What I can't figure out is where this code is getting the database context it's using at run time. If I could find that out I might be able to figure out how to circumvent that behavior and have it use a mocked/in memory DB instead.</p>
  
  <p>More explanation:
  I don't have access to a legacy database for this application right now so I'm trying to Mock up an in memory data source that gets filled from xml files. That's why I need to be able to circumvent the default database context creation.</p>
  
  <p>More Information:
  Thanks for all the help so far you wonderful people you.</p>
  
  <p>Steven seems to have directed me down the correct path. 
  In the Global.asax file is this call:</p>
  
  <pre><code>DependencyInjectorInitializer.Init();
  </code></pre>
  
  <p>Following that through the code I get to:</p>
  
  <pre><code>        public static void Initialize()
      {
          _container = new UnityContainerFactory().CreateConfiguredContainer();     
          var serviceLocator = new UnityServiceLocator(_container);
          ServiceLocator.SetLocatorProvider(() =&gt; serviceLocator);
          DependencyResolver.SetResolver(new UnityDependencyResolver(_container));
      }
  </code></pre>
  
  <p>At least that gets me headed in the right direction. Now I have to figure out how Unity is going about creating the context so I can do my intervention.</p>
  
  <ul>
  <li>Let me plug the EISK MVC Employee Info Starter Kit here. It's a well thought out system developed by Mohammad Ashraful Alam Et al. that includes a well fledged example of how many of the new technologies fit together.  MVC 5, Entity Framework 6, Unity, Authentication, OpenAuth, DI, Moq, and a couple of other things. Can be used as a template, general learning, or training.
  <a href="https://eisk.codeplex.com/" rel="nofollow">Employee Info Starter Kit</a></li>
  </ul>