Pandatech.EFCore.AuditBase is a comprehensive auditing library for Entity Framework Core, designed to make entity tracking, deletion management, and concurrency control straightforward and efficient.
To integrate Pandatech.EFCore.AuditBase into your project, install the NuGet package:
Install-Package Pandatech.EFCore.AuditBase
AuditEntityBase
in your entity classes to enable auditing.MarkAsUpdated(userId)
and MarkAsDeleted(userId)
methods to handle entity updates and deletions.DbContextExtensions.UseAuditPropertyValidation
in your DbContext to enforce audit method usage.ModelBuilderExtensions.FilterOutDeletedMarkedObjects
to automatically exclude soft-deleted entities fromAssuming you have a Product
entity in your application, you would inherit from AuditEntityBase
to include auditing
properties:
using Pandatech.VerticalSlices.Domain.Shared;
public class Product : AuditEntityBase
{
public int Id { get; set; }
public string Name { get; set; }
public decimal Price { get; set; }
}
By inheriting from AuditEntityBase, Product automatically gains auditing properties
like CreatedAt
, CreatedByUserId
, UpdatedAt
, UpdatedByUserId
, Deleted
and Version
.
MarkAsUpdated
and MarkAsDeleted
:When updating or deleting an entity, use the provided methods to ensure the audit properties are correctly updated:
public void UpdateProduct(Product product, long updatingUserId)
{
// Perform your update logic...
product.MarkAsUpdated(updatingUserId); //optional UpdatedAt DateTime parameter
_dbContext.SaveChanges();
}
public void DeleteProduct(Product product, long deletingUserId)
{
product.MarkAsDeleted(deletingUserId); //optional UpdatedAt DateTime parameter
_dbContext.SaveChanges();
}
In your DbContext
, ensure you call UseAuditPropertyValidation
to enforce the usage of audit methods and
FilterOutDeletedMarkedObjects
to apply the global filter for soft-deleted entities:
using Microsoft.EntityFrameworkCore;
using Pandatech.VerticalSlices.Infrastructure.Context;
public class MyDbContext : DbContext
{
public DbSet<Product> Products { get; set; }
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.FilterOutDeletedMarkedObjects(); // Apply global query filter for soft deletes
}
public MyDbContext(DbContextOptions<MyDbContext> options) : base(options)
{
this.UseAuditPropertyValidation(); // Enforce audit method usage
}
}
ExecuteSoftDeleteAsync
:To perform a soft delete on multiple entities, use the ExecuteSoftDeleteAsync
extension method.
public async Task SoftDeleteProductsAsync(MyDbContext dbContext, long userId)
{
var productsToSoftDelete = dbContext.Products.Where(p => p.Price > 100);
await productsToSoftDelete.ExecuteSoftDeleteAsync(userId);
}
If you need to include soft-deleted entities in a specific query, use IgnoreQueryFilters()
:
var allProductsIncludingDeleted = _dbContext.Products.IgnoreQueryFilters().ToList();
The AuditEntityBase
includes a versioning mechanism to manage concurrent updates. In the event of a conflict, a
concurrency exception will be thrown. This can be gracefully handled using try-catch blocks or integrated
with Pandatech.ResponseCrafter
for automated response management. This concurrency control is enabled by default and
is not locking the row as it uses optimistic lock.
Contributions are welcome! Please submit a pull request or open an issue to propose changes or report bugs.
Pandatech.EFCore.AuditBase is licensed under the MIT License.