title: Injecting IHttpContextAccessor into ApplicationDbContext ASP.NET Core 1.0
body: <p>I am trying to figure out how to get access to the current logged in username outside of an ASP.NET Controller.</p>
  
  <p>For example I am trying to do this:</p>
  
  <p><a href="http://benjii.me/2014/03/track-created-and-modified-fields-automatically-with-entity-framework-code-first/" rel="noreferrer">Track Created and Modified fields Automatically with Entity Framework Code First</a></p>
  
  <p>To setup tracking on entities in the <code>DbContext</code>.</p>
  
  <p>Here is my <code>ApplicationDbContext</code> but I keep getting an error saying <code>_httpContextAccessor</code> is <code>null</code>:</p>
  
  <pre><code>private readonly IHttpContextAccessor _httpContextAccessor;
  
  public ApplicationDbContext(DbContextOptions&lt;ApplicationDbContext&gt; options, IHttpContextAccessor httpContextAccessor)
      : base(options)
  {
      _httpContextAccessor = httpContextAccessor;
  }
  </code></pre>