Home > Back-end >  EF Core query on .NET Core webapi startup
EF Core query on .NET Core webapi startup

Time:09-29

Is there a way to perform a SQL query on web api startup through Entity Framework Core.

I need to load several background processes. Thanks!

Example: Startup.cs

public void ConfigureServices(IServiceCollection services)
{
    // ...
    services.AddDbContext<ApiContext>(
        options => options.UseSqlServer(
            Configuration.GetConnectionString("SqlServer"),          
            sqlServerOptionsAction: sqlOptions =>
                {
                     sqlOptions.EnableRetryOnFailure(
                         maxRetryCount: 3,
                         maxRetryDelay: TimeSpan.FromSeconds(5),
                         errorNumbersToAdd: null);
                }
        ));

    // ...
    var conts = await db.Conts.ListAsync();

    foreach(var c in conts)
    {             
        services.AddSingleton<IHostedService>(t => new Worker(c.id,(int)c.interval));
    }

    // ...
}

CodePudding user response:

Create an extension of the BackgroundService class. Create/Inject a DbContext instance and do all your ef core work inside ExecuteAsync method of the BackgroundService .

CodePudding user response:

So what you wanted to do is get the injected DbContext, right? If that's so, you cannot do that in your Startup.ConfigureServices. What you would need is a custom ProviderFactory class that will inherit from IServiceProviderFactory<IServiceCollection>:

internal class ApiServiceProviderFactory : IServiceProviderFactory<IServiceCollection>
{
    private readonly HostBuilderContext _builderContext;

    public ApiServiceProviderFactory([NotNull] HostBuilderContext hostBuilderContext)
    {
        _builderContext = hostBuilderContext;
    }

    public IServiceCollection CreateBuilder(IServiceCollection services)
    {
        return services;
    }

    public IServiceProvider CreateServiceProvider(IServiceCollection containerBuilder)
    {
        var isDevelopment = _builderContext.HostingEnvironment.IsDevelopment();

        var options = new ServiceProviderOptions
        {
            ValidateOnBuild = isDevelopment,
            ValidateScopes = isDevelopment
        };

        var serviceProvider = containerBuilder.BuildServiceProvider(options);

        // var dbFactory = serviceProvider.GetRequiredService<IDbContextFactory<TContext>>();
        // using var context = dbFactory.CreateDbContext();
        

        return serviceProvider;
    }

I may be using the IDbContextFactory from my example, but this is similar. Then use your custom ProviderFactory class in your Program.cs

public static async Task Main(string[] args)
{
    await CreateHostBuilder(args)
        .UseServiceProviderFactory(context => new ApiServiceProviderFactory(context))
        .Build()
        .RunAsync();
}
  • Related