@@ -7,6 +7,7 @@ public class SoftDeleteSaveChangesFilter<TDbContext, TUserId> : ISaveChangesFilt
7
7
where TDbContext : DbContext
8
8
where TUserId : IComparable
9
9
{
10
+ private readonly Type _userIdType ;
10
11
private readonly IUserContext ? _userContext ;
11
12
private readonly TDbContext _context ;
12
13
private readonly MasaDbContextOptions < TDbContext > _masaDbContextOptions ;
@@ -16,6 +17,7 @@ public SoftDeleteSaveChangesFilter(
16
17
TDbContext dbContext ,
17
18
IUserContext ? userContext = null )
18
19
{
20
+ _userIdType = typeof ( TUserId ) ;
19
21
_masaDbContextOptions = masaDbContextOptions ;
20
22
_context = dbContext ;
21
23
_userContext = userContext ;
@@ -38,7 +40,7 @@ public void OnExecuting(ChangeTracker changeTracker)
38
40
if ( _userContext != null && entity . Entity is IAuditEntity < TUserId > &&
39
41
entity . CurrentValues [ nameof ( IAuditEntity < TUserId > . Modifier ) ] != default )
40
42
{
41
- var userId = _userContext . UserId ;
43
+ var userId = GetUserId ( _userContext . UserId ) ;
42
44
if ( userId != null ) entity . CurrentValues [ nameof ( IAuditEntity < TUserId > . Modifier ) ] = userId ;
43
45
44
46
entity . CurrentValues [ nameof ( IAuditEntity < TUserId > . ModificationTime ) ] =
@@ -77,4 +79,16 @@ protected virtual void HandleDependent(object dependentEntry)
77
79
if ( entityEntry . Entity is ISoftDelete )
78
80
entityEntry . CurrentValues [ nameof ( ISoftDelete . IsDeleted ) ] = true ;
79
81
}
82
+
83
+ private object ? GetUserId ( string ? userId )
84
+ {
85
+ if ( userId == null )
86
+ return null ;
87
+
88
+ if ( _userIdType == typeof ( Guid ) )
89
+ {
90
+ return Guid . Parse ( userId ) ;
91
+ }
92
+ return Convert . ChangeType ( userId , _userIdType ) ;
93
+ }
80
94
}
0 commit comments