You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

55 lines
1.5 KiB
C#

using System;
using System.Threading;
using System.Threading.Tasks;
using EntrustSettle.Common;
using EntrustSettle.Common.Seed;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
namespace EntrustSettle.Extensions;
public sealed class SeedDataHostedService : IHostedService
{
private readonly SeedContext _myContext;
private readonly ILogger<SeedDataHostedService> _logger;
public SeedDataHostedService(SeedContext myContext, ILogger<SeedDataHostedService> logger)
{
_myContext = myContext;
_logger = logger;
}
public async Task StartAsync(CancellationToken cancellationToken)
{
_logger.LogInformation("Start Initialization Db Seed Service!");
await DoWork();
}
private async Task DoWork()
{
try
{
if (AppSettings.app("AppSettings", "SeedDBEnabled").ObjToBool())
{
await Task.CompletedTask;
// 初始化Main数据库结构
DBSeed.Seed(_myContext);
// 初始化日志数据库结构
DBSeed.MigrationLogs(_myContext);
}
}
catch (Exception ex)
{
_logger.LogError(ex, "Error occured seeding the Database.");
throw;
}
}
public Task StopAsync(CancellationToken cancellationToken)
{
_logger.LogInformation("Stop Initialization Db Seed Service!");
return Task.CompletedTask;
}
}