title: How do I access DataContext from another class in ASP.NET Core?
body: <p>I created a class so I can query my data. If I do it in the controller class data context works, but if I do it in my created class it throws null error.  It throws the following error:</p>
  
  <blockquote>
    <p>Microsoft.Extensions.DependencyInjection.Abstractions.dll but was not handled in user code</p>
  </blockquote>
  
  <p>My startup config:</p>
  
  <pre><code>public void ConfigureServices(IServiceCollection services)
  {
      // Add framework services.
      services.AddApplicationInsightsTelemetry(Configuration);
  
      services.AddDbContext&lt;Abstractor_DataContext&gt;(options =&gt;
         options.UseSqlServer(Configuration.GetConnectionString("ApplicationDatabase")));
  
      //services.AddTransient(typeof(Abstractor_DataContext));
  
  
      //Register your filter as a service (Note this filter need not be an attribute as such)
      services.AddTransient&lt;AppExceptionFilterAttribute&gt;();
  
      services.AddMvc();
  }
  </code></pre>
  
  <p>My class function to access the DataContext</p>
  
  <pre><code>using Microsoft.Extensions.DependencyInjection;
  public static IList&lt;ApplicationInfo&gt; TestDb()
  {
      //var options = _serviceProvider.GetService&lt;Abstractor_DataContext&gt;();
      using (var context = _serviceProvider.GetService&lt;Abstractor_DataContext&gt;())
      {
          return context.ApplicationInfo.ToList();
      }
  }
  </code></pre>
  
  <p><code>var context</code> is null.  What am I missing? I am slowly learning DI. </p>