Wie greife ich von einer anderen Klasse in ASP.NET Core auf DataContext zu?

Ich habe eine Klasse erstellt, damit ich meine Daten abfragen kann. Wenn ich es im Kontext der Controller-Klassendaten mache, funktioniert es, aber wenn ich es in meiner erstellten Klasse mache, gibt es einen Null-Fehler aus. Es wird der folgende Fehler ausgegeben:

Microsoft.Extensions.DependencyInjection.Abstractions.dll wurde jedoch nicht im Benutzercode behandelt

Meine Startkonfiguration:

public void ConfigureServices(IServiceCollection services)
{
    // Add framework services.
    services.AddApplicationInsightsTelemetry(Configuration);

    services.AddDbContext<Abstractor_DataContext>(options =>
       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<AppExceptionFilterAttribute>();

    services.AddMvc();
}

Meine Klassenfunktion für den Zugriff auf den DataContext

using Microsoft.Extensions.DependencyInjection;
public static IList<ApplicationInfo> TestDb()
{
    //var options = _serviceProvider.GetService<Abstractor_DataContext>();
    using (var context = _serviceProvider.GetService<Abstractor_DataContext>())
    {
        return context.ApplicationInfo.ToList();
    }
}

var context ist Null. Was vermisse ich? Ich lerne langsam DI.

Antworten auf die Frage(4)

Ihre Antwort auf die Frage