From b2ad000e07e1c87d3561b5aa94fdc88c779872f0 Mon Sep 17 00:00:00 2001 From: dengjunjie <dengjunjie@hnkhzn.com> Date: 星期二, 18 二月 2025 22:34:54 +0800 Subject: [PATCH] 1 --- 项目代码/WMS/WIDESEA_WMSServer/WIDESEA_Core/HostedService/SeedDataHostedService.cs | 73 ++++++++++++++++++++++++++++++++++++ 1 files changed, 73 insertions(+), 0 deletions(-) diff --git "a/\351\241\271\347\233\256\344\273\243\347\240\201/WMS/WIDESEA_WMSServer/WIDESEA_Core/HostedService/SeedDataHostedService.cs" "b/\351\241\271\347\233\256\344\273\243\347\240\201/WMS/WIDESEA_WMSServer/WIDESEA_Core/HostedService/SeedDataHostedService.cs" new file mode 100644 index 0000000..2b7ddd3 --- /dev/null +++ "b/\351\241\271\347\233\256\344\273\243\347\240\201/WMS/WIDESEA_WMSServer/WIDESEA_Core/HostedService/SeedDataHostedService.cs" @@ -0,0 +1,73 @@ +锘縰sing Microsoft.AspNetCore.Hosting; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Hosting; +using Microsoft.Extensions.Logging; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using WIDESEA_Core.Helper; +using WIDESEA_Core.Seed; + +namespace WIDESEA_Core +{ + public sealed class SeedDataHostedService : IHostedService + { + private readonly DBContext _dbContext; + private readonly ILogger<SeedDataHostedService> _logger; + private readonly string _webRootPath; + private readonly IServiceProvider _serviceProvider; + + public SeedDataHostedService( + IServiceProvider serviceProvider, + IWebHostEnvironment webHostEnvironment, + ILogger<SeedDataHostedService> logger) + { + _serviceProvider = serviceProvider; + _logger = logger; + _webRootPath = webHostEnvironment.WebRootPath; + + using var scope = _serviceProvider.CreateScope(); + + var dbContext = scope.ServiceProvider.GetService<DBContext>(); + //dbContext.Db.Aop.DataExecuting = SqlSugarAop.DataExecuting; + _dbContext = dbContext; + } + + public Task StartAsync(CancellationToken cancellationToken) + { + _logger.LogInformation("Start Initialization Db Seed Service!"); + DoWork(); + return Task.CompletedTask; + } + + private void DoWork() + { + try + { + //if (AppSettings.app("AppSettings", "SeedDBEnabled").ObjToBool() || AppSettings.app("AppSettings", "SeedDBDataEnabled").ObjToBool()) + { + // 浣跨敤 myScopedService 鎵ц浠诲姟 + + DBSeed.SeedAsync(_dbContext, _webRootPath); + + //澶氱鎴� 鍚屾 + //await DBSeed.TenantSeedAsync(_dbContext); + } + } + 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; + } + + } +} -- Gitblit v1.9.3