ChangeTracker.Entries()allows you to get all entity changes. You can override SaveChangesin your DbContext and set the changed properties using something like the following code.
public override int SaveChanges()
{
SetModifiedInformation();
return base.SaveChanges();
}
public override async Task<int> SaveChangesAsync( CancellationToken cancellationToken = new CancellationToken() )
{
SetModifiedInformation();
return await base.SaveChangesAsync( cancellationToken );
}
private void SetModifiedInformation()
{
foreach (var entityEntry in ChangeTracker.Entries())
{
var entity = entityEntry.Entity as ChangeTracking;
if (entity != null)
{
entity.ModifiedBy = "Get User Here";
entity.ModifiedTime = DateTime.Now;
}
}
}
source
share