title: EF Core Migrations in Azure Function startup
body: <p>According to <a href="https://docs.microsoft.com/en-us/azure/azure-functions/functions-dotnet-dependency-injection" rel="noreferrer">https://docs.microsoft.com/en-us/azure/azure-functions/functions-dotnet-dependency-injection</a> the service provider should not be used until AFTER the startup has completed running. Indeed, if I try to get a registered service it will fail.</p>
  <p>Example:</p>
  <pre class="lang-cs prettyprint-override"><code>[assembly: FunctionsStartup(typeof(Startup))]
  
  namespace Fx {
      public sealed class Startup : FunctionsStartup {
          public override void Configure(IFunctionsHostBuilder builder) {
              var configurationBuilder = new ConfigurationBuilder();
              configurationBuilder.AddEnvironmentVariables();
  
              var configuration = configurationBuilder.Build();
  
              builder.Services.AddInfrastructure(configuration);
              builder.Services.AddApplication();
  
              var serviceProvider = builder.Services.BuildServiceProvider();
              DependencyInjection.AddDatabase(serviceProvider).GetAwaiter().GetResult();
          }
      }
  }
  </code></pre>
  <pre class="lang-cs prettyprint-override"><code>    public static class DependencyInjection {
          public static async Task AddDatabase(IServiceProvider services) {
              using var scope = services.CreateScope();
  
              var serviceProvider = scope.ServiceProvider;
  
              var context = serviceProvider.GetRequiredService&lt;ApplicationDbContext&gt;();
              //Error generated here
              if (context.Database.IsSqlServer()) {
                  await context.Database.MigrateAsync();
              }
              await ApplicationDbContextSeed.SeedSamplePersonnelDataAsync(context);
          }
  
          public static IServiceCollection AddInfrastructure(
              this IServiceCollection services,
              IConfiguration configuration) {
              services.AddDbContext&lt;ApplicationDbContext&gt;(options =&gt;
                  options.UseSqlServer(configuration.GetConnectionString(&quot;DefaultConnection&quot;),
                      b =&gt; b.MigrationsAssembly(typeof(ApplicationDbContext).Assembly.FullName)));
  
              services.AddScoped&lt;IApplicationDbContext&gt;(provider =&gt; provider.GetService&lt;ApplicationDbContext&gt;());
  
              return services;
          }
      }
  </code></pre>
  <p>This produces the following error</p>
  <pre><code>Microsoft.EntityFrameworkCore: No database provider has been configured for this DbContext. A provider can be configured by overriding the DbContext.OnConfiguring method or by using AddDbContext on the application service provider. If AddDbContext is used, then also ensure that your DbContext type accepts a DbContextOptions&lt;TContext&gt; object in its constructor and passes it to the base constructor for DbContext.
  </code></pre>
  <p>Is there a good option for migrating and seeding during startup?</p>