using System; using System.Threading; using System.Threading.Tasks; using EntityFrameworkCore.Triggered; using IRaCIS.Core.Domain.Models; using IRaCIS.Core.Domain.Share; namespace IRaCIS.Core.Application.Triggers { public class SoftDeleteTrigger : IBeforeSaveTrigger { private readonly IUserInfo _userInfo; public SoftDeleteTrigger(IUserInfo userInfo) { _userInfo = userInfo; } //Generator Detached 状态才会进去 误用 //modelBuilder.Entity(entityType.ClrType).Property(nameof(ISoftDelete.DeletedTime)).HasValueGenerator().ValueGeneratedOnAddOrUpdate(); public Task BeforeSave(ITriggerContext context, CancellationToken cancellationToken) { if (context.ChangeType == ChangeType.Modified) { if (context.Entity.IsDeleted) { context.Entity.DeleteUserId = _userInfo.Id; context.Entity.DeletedTime = DateTime.UtcNow.AddHours(8); } else { context.Entity.DeletedTime = null; } } return Task.CompletedTask; } } }