dotnetcore / dotnetcore/FreeSql

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

Open
#2,120 1 comment 0 reactions 0 assignees View on GitHub
Dominant language
C#
Stars
4.4k
Forks
910
PR merge metrics
No merged PRs in 30d

Description

#### 问题描述及重现代码:

```c#
// c# code
///
/// 国家
///
public sealed class CountryStoreModel : StoreModelMultiTenantsHasFullAudit
{
///
/// 国旗
///
public string Flag { get; set; } = string.Empty;
///
/// 国旗类型
///
public TextTypes FlagType { get; set; }
///
/// 名称
///
public string Name { get; set; } = string.Empty;
///
/// 编码
///
public string Code { get; set; } = string.Empty;
///
/// 是否热门
///
public bool IsHot { get; set; }
///
/// 序号
///
public short Order { get; set; }
}
await MySQL.Update().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();
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.TenantId))
e.Value = account.TenantId();
};
return it;
}
}
```

#### 数据库版本
8.0.22

#### 安装的Nuget包

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

Contributor guide

No contributing guide indexed for this repository

Research direction

Start with the IAopExtensions.TrySetAuditing handler shown in the issue and the Update() call in the reproduction. Run the example against MySQL 8.0.22 on .NET 9, then trace whether the update reaches AuditValueType.Update and whether TrySetModification changes the command. Done means the update triggers auditing and applies the expected modification values.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp, mysql
Domain
backend, database
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
40/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.