Skip to content

update 时 fsql.Aop.AuditValue 自动审计值不触发 #2120

@justmine2514

Description

@justmine2514

问题描述及重现代码:

// c# code
/// <summary>
/// 国家
/// </summary>
public sealed class CountryStoreModel : StoreModelMultiTenantsHasFullAudit<long>
{
    /// <summary>
    /// 国旗
    /// </summary>
    public string Flag { get; set; } = string.Empty;
    /// <summary>
    /// 国旗类型
    /// </summary>
    public TextTypes FlagType { get; set; }
    /// <summary>
    /// 名称
    /// </summary>
    public string Name { get; set; } = string.Empty;
    /// <summary>
    /// 编码
    /// </summary>
    public string Code { get; set; } = string.Empty;
    /// <summary>
    /// 是否热门
    /// </summary>
    public bool IsHot { get; set; }
    /// <summary>
    /// 序号
    /// </summary>
    public short Order { get; set; }
}
await MySQL.Update<CountryStoreModel>().Where(it => it.Id == @event.EntityId)
            .Set(p => p.Code, @event.Code)
            .Set(p => p.Name, @event.Name)
            .Set(p => p.IsHot, @event.IsHot)
            .Set(p => p.Flag, @event.Flag)
            .Set(p => p.Order, @event.Order).ExecuteAffrowsAsync(token);
 public static DependencyInjectionContext TryAddFreeSQL(this DDDDependencyInjectionContext context, string connectionStringName,
     bool printSQL = false,
     bool autoSyncStructure = false,
     bool useNoneCommandParameter = true,
     bool enableAuditing = false,
     bool enableMultiTenants = false,
     bool enableSoftDeletion = false,
     ServiceLifetime lifetime = ServiceLifetime.Singleton) => context.TryAddFreeSQL(connectionStringName, (_, builder) =>
     {
         builder.UseAutoSyncStructure(autoSyncStructure).UseNoneCommandParameter(useNoneCommandParameter);
         if (printSQL) builder.UseMonitorCommand(cmd => Console.WriteLine($"{cmd.CommandText}{Console.Out.NewLine}"));
     }, (services, freeSQL) =>
     {
         if (enableAuditing) freeSQL.Aop.TrySetAuditing(services, enableMultiTenants);
         if (enableSoftDeletion) freeSQL.GlobalFilter.ApplySoftDeletion();
         if (enableMultiTenants) freeSQL.GlobalFilter.ApplyMultiTenants();
     }, lifetime: lifetime);
public static class IAopExtensions
{
    public static IAop TrySetAuditing(this IAop it, IServiceProvider services, bool enableMultiTenants = false)
    {
        it.AuditValue += (s, e) =>
        {
            using var scope = services.CreateScope();
            var account = scope.GetService<ICurrentAccount>();
            if (account == null) return;
            e.TrySetOperation(account);
            switch (e.AuditValueType)
            {
                case AuditValueType.Insert:
                    e.TrySetCreation(account);
                    break;
                case AuditValueType.Update:
                    e.TrySetModification(account);
                    break;
                case AuditValueType.InsertOrUpdate:
                    e.TrySetCreation(account).TrySetModification(account);
                    break;
            }
            if (enableMultiTenants && !account.IsSuper 
            && e.Property.PropertyType == typeof(long) 
            && e.Property.Name == nameof(IMultiTenants<long>.TenantId))
                e.Value = account.TenantId();
        };
        return it;
    }
}

数据库版本

8.0.22

安装的Nuget包

.net framework/. net core? 及具体版本

.net9

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions