@@ -27,6 +27,8 @@ public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options, IHtt
2727 _httpContext = httpContextAccessor ;
2828 }
2929
30+ public DbSet < AutoHistory > AutoHistory { get ; set ; } = null ! ;
31+
3032 public DbSet < RolePermission > RolePermission { get ; set ; } = null ! ;
3133 public DbSet < WikiPage > WikiPages { get ; set ; } = null ! ;
3234 public DbSet < WikiPageReferral > WikiReferrals { get ; set ; } = null ! ;
@@ -94,16 +96,65 @@ public override int SaveChanges(bool acceptAllChangesOnSuccess)
9496 PerformTrackingUpdates ( ) ;
9597
9698 ChangeTracker . AutoDetectChangesEnabled = false ;
99+
100+ // remember added entries,
101+ // before EF Core is assigning valid Ids (it does on save changes,
102+ // when ids equal zero) and setting their state to
103+ // Unchanged (it does on every save changes)
104+ var addedEntities = ChangeTracker
105+ . Entries ( )
106+ . Where ( e => e . State == EntityState . Added )
107+ . ToArray ( ) ;
108+
109+ this . EnsureAutoHistory ( ( ) => new CustomAutoHistory ( )
110+ {
111+ UserId = _httpContext ? . HttpContext ? . User . GetUserId ( ) ?? - 1
112+ } ) ;
97113 var result = base . SaveChanges ( acceptAllChangesOnSuccess ) ;
114+
115+ // after "SaveChanges" added enties now have gotten valid ids (if it was necessary)
116+ // and the history for them can be ensured and be saved with another "SaveChanges"
117+ this . EnsureAddedHistory (
118+ ( ) => new CustomAutoHistory ( )
119+ {
120+ UserId = _httpContext ? . HttpContext ? . User . GetUserId ( ) ?? - 1
121+ } , addedEntities ) ;
122+ result += base . SaveChanges ( acceptAllChangesOnSuccess ) ;
123+
98124 ChangeTracker . AutoDetectChangesEnabled = true ;
99125
100126 return result ;
101127 }
102128
103- public override Task < int > SaveChangesAsync ( CancellationToken cancellationToken = default )
129+ public override async Task < int > SaveChangesAsync ( CancellationToken cancellationToken = default )
104130 {
105131 PerformTrackingUpdates ( ) ;
106- return base . SaveChangesAsync ( cancellationToken ) ;
132+
133+ // remember added entries,
134+ // before EF Core is assigning valid Ids (it does on save changes,
135+ // when ids equal zero) and setting their state to
136+ // Unchanged (it does on every save changes)
137+ var addedEntities = ChangeTracker
138+ . Entries ( )
139+ . Where ( e => e . State == EntityState . Added )
140+ . ToArray ( ) ;
141+
142+ this . EnsureAutoHistory ( ( ) => new CustomAutoHistory ( )
143+ {
144+ UserId = _httpContext ? . HttpContext ? . User . GetUserId ( ) ?? - 1
145+ } ) ;
146+ var result = await base . SaveChangesAsync ( cancellationToken ) ;
147+
148+ // after "SaveChanges" added enties now have gotten valid ids (if it was necessary)
149+ // and the history for them can be ensured and be saved with another "SaveChanges"
150+ this . EnsureAddedHistory (
151+ ( ) => new CustomAutoHistory ( )
152+ {
153+ UserId = _httpContext ? . HttpContext ? . User . GetUserId ( ) ?? - 1
154+ } , addedEntities ) ;
155+ result += await base . SaveChangesAsync ( CancellationToken . None ) ;
156+
157+ return result ;
107158 }
108159
109160 /// <summary>
@@ -421,6 +472,11 @@ protected override void OnModelCreating(ModelBuilder builder)
421472 {
422473 entity . HasIndex ( e => e . FileExtension ) . IsUnique ( ) ;
423474 } ) ;
475+
476+ builder . EnableAutoHistory < CustomAutoHistory > ( o =>
477+ {
478+ o . LimitChangedLength = false ;
479+ } ) ;
424480 }
425481
426482 private void PerformTrackingUpdates ( )
0 commit comments