How to exclude tracked entities from audit? #506
-
Hello, For instance, we have the following background task (it is our custom solution, also with a custom repository wrapper around DbContext):
Basically, I don't want the audit logs to be generated just for this background task. In other places (other jobs, controllers etc.) the audit should happen normally. Is there any way to disable the logs just for a given transaction or a tracked entity? I know there is a We are configuring the Audit.NET this way:
and our database context is derived from Thank you and have a nice day. |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
Can you share the code of your IRepository and also the relevant code on your DbContext? Note you can set the Another option could be to define a specific DbContext class that inherits from your DbContext but bypasses the audits: public class NoAuditDbContext : YourDbContext
{
public NoAuditDbContext(DbContextOptions options) : base(options) { }
public override int SaveChanges() => ((IAuditBypass)this).SaveChangesBypassAudit();
public override int SaveChanges(bool acceptAllChangesOnSuccess) => ((IAuditBypass)this).SaveChangesBypassAudit();
public override Task<int> SaveChangesAsync(CancellationToken cancellationToken = default) => ((IAuditBypass)this).SaveChangesBypassAuditAsync();
public override Task<int> SaveChangesAsync(bool acceptAllChangesOnSuccess, CancellationToken cancellationToken = default) => ((IAuditBypass)this).SaveChangesBypassAuditAsync();
} |
Beta Was this translation helpful? Give feedback.
-
@thepirat000 thank you, I think that's all I needed - I will discuss the final solution with my colleague. So far I have tried the The transaction interface: public interface ITransaction
{
Task CommitAsync();
int Commit();
bool AuditDisabled { get; set; }
} Entity Framework transaction implementation: public class EfTransaction<TContext> : ITransaction
where TContext : DbContext
{
private readonly TContext _context;
public EfTransaction(TContext context)
{
_context = context;
}
public TContext Context => _context;
public Task CommitAsync()
{
if (AuditDisabled && _context is AuditDbContext auditContext)
{
auditContext.AuditDisabled = true;
}
return _context.SaveChangesAsync();
}
public int Commit()
{
if (AuditDisabled && _context is AuditDbContext auditContext)
{
auditContext.AuditDisabled = true;
}
return _context.SaveChanges();
}
public bool AuditDisabled { get; set; }
} The repository implementation: public class EfRepository<TEntity, TContext> : IRepository<TEntity>
where TEntity : class
where TContext : DbContext
{
private readonly EfTransaction<TContext> _transaction;
public ITransaction Transaction => _transaction;
public EfRepository(EfTransaction<TContext> transaction)
{
_transaction = transaction;
}
public EntityEntry<TEntity> Add(TEntity item)
{
return _transaction.Context.Add(item);
}
public IQueryable<TEntity> GetAll()
{
return _transaction.Context.Set<TEntity>();
}
// other methods...
} The database context: public class AppDbContext : AuditDbContext
{
// nothing special here...
} An example usage within the background job: var user = await _userService.GetByIdAsync(1, cancellationToken);
user!.UserName = "thepirat000";
_transaction.AuditDisabled = true;
await _transaction.CommitAsync(); |
Beta Was this translation helpful? Give feedback.
Can you share the code of your IRepository and also the relevant code on your DbContext?
Please also include the Dependency Injection relevant configuration.
Note you can set the
AuditDisabled
property totrue
on theAuditDbContext
instance(s) used by your background job, but it will depend on how you create the DbContext instance on your repository.Another option could be to define a specific DbContext class that inherits from your DbContext but bypasses the audits: