You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Hi, i couldn't find anything about this so im asking.
Im trying to make a Before trigger that saves a json serialized version of marked entities into an audit table.
The trigger is being called correctly for the marked entities.
The audit entry is created correctly.
But then the call to dbContext.AddAsync seems to have no effect.
This is inside of an aspnet core web api, with a scoped dbContext.
I would have expected the auditEntry to be saved in the database, but the audit table is empty afterwards.
I appreciate any help, hints or clarifications.
Thank you in advance.
Here is my code:
public class AuditTrigger<T>(
AppDbContext dbContext,
IJsonService jsonService,
IClock clock
) : IBeforeSaveTrigger<T>
where T : class, IAuditableEntity
{
public async Task BeforeSave(ITriggerContext<T> context, CancellationToken cancellationToken)
{
var entityType = dbContext.Model.FindEntityType(context.Entity.GetType())!;
var auditEntry = new AuditEntry
{
Timestamp = clock.GetCurrentInstant(),
TransactionId = dbContext.Database.CurrentTransaction?.TransactionId ?? Guid.Empty,
Type = GetEntryType(context.ChangeType),
Table = entityType.GetTableName()!,
EntityId = context.Entity.GetIdAsGuid(),
Details = jsonService.SerializeToDocument(context.Entity, entityType.ClrType)
};
await dbContext.AddAsync(auditEntry, cancellationToken);
}
private AuditEntryType GetEntryType(ChangeType changeType)
{
switch (changeType)
{
case ChangeType.Added:
return AuditEntryType.Insert;
case ChangeType.Modified:
return AuditEntryType.Update;
case ChangeType.Deleted:
return AuditEntryType.Delete;
}
throw new UnreachableException();
}
}
public static class AuditTriggerExtensions
{
public static IServiceCollection AddAuditTriggers(this IServiceCollection serviceCollection)
{
var auditableEntities = typeof(AppDbContext).Assembly
.GetTypes()
.Where(x => x.IsAbstract == false)
.Where(x => x.IsAssignableTo(typeof(IAuditableEntity)))
.ToList();
var interfaceType = typeof(IBeforeSaveTrigger<>);
var auditTriggerType = typeof(AuditTrigger<>);
foreach (var auditableEntity in auditableEntities)
{
var genericInterfaceType = interfaceType.MakeGenericType(auditableEntity);
var genericTriggerType = auditTriggerType.MakeGenericType(auditableEntity);
serviceCollection.AddTransient(genericInterfaceType, genericTriggerType);
}
return serviceCollection;
}
}
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
Hi, i couldn't find anything about this so im asking.
Im trying to make a Before trigger that saves a json serialized version of marked entities into an audit table.
The trigger is being called correctly for the marked entities.
The audit entry is created correctly.
But then the call to dbContext.AddAsync seems to have no effect.
This is inside of an aspnet core web api, with a scoped dbContext.
I would have expected the
auditEntry
to be saved in the database, but the audit table is empty afterwards.I appreciate any help, hints or clarifications.
Thank you in advance.
Here is my code:
Beta Was this translation helpful? Give feedback.
All reactions