【问题标题】:How to seed Hangfire database in asp.net core 2.0如何在 asp.net core 2.0 中播种 Hangfire 数据库
【发布时间】:2017-12-05 21:03:44
【问题描述】:

我正在尝试将“Hangfire”集成到我现有的 ASP.NET Core 应用程序中。查看文档,如果我们想使用 SQL Server 存储选项,似乎需要创建 Hangfire db。但是每次我们启动一个新服务器时创建一个数据库真的很烦人。

有没有一种方法可以在启用 SQL 存储选项的情况下为 DB 播种,而不是像这样从启动类中播种?

 services.AddHangfire(options =>
            options.UseSqlServerStorage(Configuration["HangfireConnection"]));

【问题讨论】:

    标签: asp.net-core asp.net-core-2.0 hangfire hangfire.ninject


    【解决方案1】:

    一个好的解决方案可能是将这个问题与一些 TSQL 脚本隔离开来,并在应用程序启动或运行测试之前运行这些脚本(基于您的场景),但我之前使用过的快速解决方案类似于 @ 987654321@.

    public class HangfireContext : DbContext
    {
        public HangfireContext(DbContextOptions options)
            : base(options)
        {
            Database.EnsureCreated();
        }
    }
    
    
    public class Startup
    {
    
       public void ConfigureServices(IServiceCollection services)
       {
          var connectionString = Configuration.GetConnectionString("HangfireConnectionString");
          var optionsBuilder = new DbContextOptionsBuilder<HangfireContext>();
          optionsBuilder.UseSqlServer(connectionString);
          var hangfireDbContext = new HangfireContext(optionsBuilder.Options);
    
          services.AddHangfire(config =>
                    config.UseSqlServerStorage(connectionString));
       }
    }
    

    【讨论】:

    • 谢谢!!我将它用于我的集成测试:在执行 EnsureCreated 之前,我还运行 EnsureDeleted 以每次都从头开始
    猜你喜欢
    • 1970-01-01
    • 2011-08-29
    • 2023-03-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-02-09
    • 2018-03-15
    相关资源
    最近更新 更多