-
Notifications
You must be signed in to change notification settings - Fork 29
Catch all triggers
Koen edited this page Nov 29, 2020
·
1 revision
All entities have a common base type, which is Object
. Imagine we want to log what Entity got changed and when:
public class LogTrigger : IAfterSaveTrigger<object> {
readonly IApplicationDbContext _applicationDbContext;
public LogTrigger(ApplicationDbContext applicationDbContext) {
_applicationDbContext = applicationDbContext;
}
public Task AfterSave(ITriggerContext<object> context, CancellationToken cancellationToken) {
var entry = _applicationDbContext.ChangeTracker.Entry(context.Entity);
var keyString = string.Join(", ", entry.Metadata.FindPrimaryKey()
.Properties.Select(p => entry.Property(p.Name).CurrentValue));
Console.WriteLine($"Detected {context.ChangeType} on a {context.Entity.GetType().Name} with id: {keyString}");
}
}