Add migration error: No database provider has been configured for this DbContext

Add migration error: No database provider has been configured for this DbContext

添加迁移和更新数据库工作正常,但在更新到 Entity Framework 核心的最新版本后似乎遇到了障碍。 我错过了什么?

命令失败

add-migration authenticationtoken -Context ApplicationDbContext -verbose

ApplicationDbContext.cs

public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
  {
    // I added this constructor after add-migration complained about 
    //needing a paramterless contructor.     
    public ApplicationDbContext()
    {
    }

    public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
        : base(options)
    {
    }
   ...................
}

Startup.cs

public void ConfigureServices(IServiceCollection services)
    {
      services.AddDbContext<ApplicationDbContext>(options=>       options.UseSqlServer(Configuration.GetConnectionString("SQLServerConnectionString")));
..............
}

Program.cs

public class Program
  {
    public static void Main(string[] args)
    {
      try
      {
        WebHost.CreateDefaultBuilder(args)
          .UseStartup<Startup>()
          .UseSerilog()
          .Build()
          .Run();
      }
      finally
      {
        Log.CloseAndFlush();
      }
    }
  }

project.csproj:

<Project Sdk="Microsoft.NET.Sdk.Web">
  <PropertyGroup>
    <TargetFramework>netcoreapp2.2</TargetFramework>
    <LangVersion>default</LangVersion>
  </PropertyGroup>
  <ItemGroup>
    <PackageReference Include="Braintree" Version="4.11.0" />
    <PackageReference Include="BuildBundlerMinifier" Version="2.9.406" />
    <PackageReference Include="Hangfire" Version="1.7.3" />
    <PackageReference Include="Microsoft.AspNetCore.App" />
    <PackageReference Include="Microsoft.AspNetCore.Razor.Design" Version="2.2.0" PrivateAssets="All" />
    <PackageReference Include="Microsoft.Azure.Cosmos.Table" Version="1.0.1" />
    <PackageReference Include="Microsoft.Azure.Storage.Blob" Version="10.0.3" />
    <PackageReference Include="Microsoft.Azure.Storage.Common" Version="10.0.3" />
    <PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="2.2.3" />
    <PackageReference Include="morelinq" Version="3.1.1" />
    <PackageReference Include="Newtonsoft.Json" Version="12.0.2" />
    <PackageReference Include="Sendgrid" Version="9.11.0" />
    <PackageReference Include="Serilog.AspNetCore" Version="2.1.1" />
    <PackageReference Include="Serilog.Extensions.Logging" Version="2.0.4" />
    <PackageReference Include="Serilog.Settings.AppSettings" Version="2.2.2" />
    <PackageReference Include="Serilog.Settings.Configuration" Version="3.0.1" />
    <PackageReference Include="Serilog.Sinks.MSSqlServer" Version="5.1.2" />
    <PackageReference Include="Stripe.net" Version="26.0.0" />
    <PackageReference Include="Syncfusion.EJ2.AspNet.Core" Version="17.1.0.48" />
  </ItemGroup>
</Project>

堆栈跟踪:

System.InvalidOperationException: 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<TContext> object in its constructor and passes it to the base constructor for DbContext.
   at Microsoft.EntityFrameworkCore.Internal.DbContextServices.Initialize(IServiceProvider scopedProvider, IDbContextOptions contextOptions, DbContext context)
   at Microsoft.EntityFrameworkCore.DbContext.get_InternalServiceProvider()
   at Microsoft.EntityFrameworkCore.DbContext.Microsoft.EntityFrameworkCore.Infrastructure.IInfrastructure<System.IServiceProvider>.get_Instance()
   at Microsoft.EntityFrameworkCore.Internal.InternalAccessorExtensions.GetService[TService](IInfrastructure`1 accessor)
   at Microsoft.EntityFrameworkCore.Infrastructure.AccessorExtensions.GetService[TService](IInfrastructure`1 accessor)
   at Microsoft.EntityFrameworkCore.Design.Internal.DbContextOperations.CreateContext(Func`1 factory)
   at Microsoft.EntityFrameworkCore.Design.Internal.DbContextOperations.CreateContext(String contextType)
   at Microsoft.EntityFrameworkCore.Design.Internal.MigrationsOperations.AddMigration(String name, String outputDir, String contextType)
   at Microsoft.EntityFrameworkCore.Design.OperationExecutor.AddMigrationImpl(String name, String outputDir, String contextType)
   at Microsoft.EntityFrameworkCore.Design.OperationExecutor.AddMigration.<>c__DisplayClass0_1.<.ctor>b__0()
   at Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.<>c__DisplayClass3_0`1.<Execute>b__0()
   at Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.Execute(Action action)
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<TContext> object in its constructor and passes it to the base constructor for DbContext.

感谢@ivanStoev link,我能够通过添加 public 静态 CreateWebHostBuilder 方法(我错误地重构了)来解决问题。

Program.cs:

public class Program
  {
    public static void Main(string[] args)
    {
      try
      {
        var iWebHost = CreateWebHostBuilder(args).Build();
        Log.Information("Application starting");
        iWebHost.Run();
      }
      catch (Exception exception)
      {
        Log.Error(exception.ToString());
      }
      finally
      {
        Log.CloseAndFlush();
      }
    }
    public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
        WebHost.CreateDefaultBuilder(args)
        .UseStartup<Startup>()
        .UseSerilog();
  }