33 lines
1.0 KiB
C#
33 lines
1.0 KiB
C#
using System;
|
|
using System.Threading;
|
|
using System.Threading.Tasks;
|
|
using EntityFrameworkCore.Triggered;
|
|
using IRaCIS.Core.Domain.Models;
|
|
|
|
namespace IRaCIS.Core.Application.Triggers
|
|
{
|
|
|
|
public class SoftDeleteTrigger : IBeforeSaveTrigger<ISoftDelete>
|
|
{
|
|
|
|
//Generator Detached 状态才会进去 误用
|
|
//modelBuilder.Entity(entityType.ClrType).Property(nameof(ISoftDelete.DeletedTime)).HasValueGenerator<DeleteTimeGenerator>().ValueGeneratedOnAddOrUpdate();
|
|
public Task BeforeSave(ITriggerContext<ISoftDelete> context, CancellationToken cancellationToken)
|
|
{
|
|
if (context.ChangeType == ChangeType.Modified)
|
|
{
|
|
if (context.Entity.IsDeleted)
|
|
{
|
|
context.Entity.DeletedTime=DateTime.UtcNow.AddHours(8);
|
|
}
|
|
else
|
|
{
|
|
context.Entity.DeletedTime = null;
|
|
}
|
|
}
|
|
|
|
return Task.CompletedTask;
|
|
|
|
}
|
|
}
|
|
} |