EntityFrameworkCore.AuditInterceptor 1.0.0

dotnet add package EntityFrameworkCore.AuditInterceptor --version 1.0.0                
NuGet\Install-Package EntityFrameworkCore.AuditInterceptor -Version 1.0.0                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="EntityFrameworkCore.AuditInterceptor" Version="1.0.0" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add EntityFrameworkCore.AuditInterceptor --version 1.0.0                
#r "nuget: EntityFrameworkCore.AuditInterceptor, 1.0.0"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install EntityFrameworkCore.AuditInterceptor as a Cake Addin
#addin nuget:?package=EntityFrameworkCore.AuditInterceptor&version=1.0.0

// Install EntityFrameworkCore.AuditInterceptor as a Cake Tool
#tool nuget:?package=EntityFrameworkCore.AuditInterceptor&version=1.0.0                

publish to nuget EntityFrameworkCore.AuditInterceptor on NuGet NuGet License paypal

EntityFrameworkCore.AuditInterceptor

📌 Introduction

EntityFrameworkCore.AuditInterceptor is a .NET library designed to provide seamless auditing capabilities for Entity Framework Core. It allows you to automatically track changes to your entities, including who made the changes and when they were made. The library integrates effortlessly with .NET Dependency Injection and supports various auditing scenarios, making it an ideal choice for enterprise applications that require robust auditing features.

📌 Key Features:

  • Automatic tracking of entity changes
  • Integration with .NET Dependency Injection
  • Support for various auditing scenarios
  • Easy configuration and setup

📥 Installation

To install the package, use the following command:

dotnet add package EntityFrameworkCore.AuditInterceptor

🚀 Quick Start:

Below is an example of how you might configure the auditing services and your DbContext in your Program.cs:

// Program.cs
var builder = WebApplication.CreateBuilder(args);

// Add services to the container.
builder.Services.AddRazorPages();

ConfigurationManager configuration = builder.Configuration;
string connectionString = configuration.GetConnectionString("DefaultConnection")!;
builder.Services
  .AddHttpContextAccessor()
  .AddAuditing() // Registers the required services and options
  .AddDbContext<MyDbContext>(options =>
    options.UseSqlServer(connectionString)
      .AddAuditInterceptors())
  .AddLogging(configure => configure.AddConsole());

var app = builder.Build();

using (var scope = app.Services.CreateScope())
{
  var dbContext = scope.ServiceProvider.GetRequiredService<MyDbContext>();
  dbContext.Database.Migrate();
}

// Configure the HTTP request pipeline.
if (!app.Environment.IsDevelopment())
{
  app.UseExceptionHandler("/Error");
  app.UseHsts();
}

app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
app.UseAuthorization();
app.MapRazorPages();
app.Run();

After configuring the services, you can begin creating auditable entities by implementing the IAuditable interface. Use the built-in SaveChanges or SaveChangesAsync methods to automatically track who created or updated the entities, along with timestamps.

// MyEntity.cs
using EntityFrameworkCore.AuditInterceptor.Interfaces;

public class MyEntity : IAuditable
{
  public string Id { get; set; } = string.Empty;
  public string CreatedBy { get; set; } = string.Empty;
  public DateTime CreatedAt { get; set; }
  public string UpdatedBy { get; set; } = string.Empty;
  public DateTime UpdatedAt { get; set; }
  public string Name { get; set; } = string.Empty;
}
// MyDbContext.cs
using Microsoft.EntityFrameworkCore;

public class MyDbContext : DbContext
{
  public DbSet<MyEntity> MyEntities { get; set; }

  public MyDbContext(DbContextOptions<MyDbContext> options)
    : base(options)
  {
  }
}
// Index.cshtml.cs
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;

public class IndexModel : PageModel
{
  private readonly ILogger<IndexModel> _logger;
  private readonly MyDbContext _dbContext;

  public IndexModel(ILogger<IndexModel> logger, MyDbContext dbContext)
  {
    _logger = logger;
    _dbContext = dbContext;
  }

  public void OnGet()
  {
  }

  public async Task<IActionResult> OnPostAddEntityAsync()
  {
    var newEntity = new MyEntity
    {
      Name = "Test"
    };

    _dbContext.MyEntities.Add(newEntity);
    await _dbContext.SaveChangesAsync();

    return RedirectToPage();
  }
}
// Index.cshtml
@page
@model IndexModel
@{
  ViewData["Title"] = "Home page";
}

<h1>Home Page</h1>

<form method="post">
  <button type="submit" asp-page-handler="AddEntity">Add New Entity</button>
</form>

🔗 License

This project is licensed under the MIT License. See the LICENSE file for details.


🙌 Contributing

🎯 Found a bug or have an idea for improvement?
Feel free to open an issue or submit a pull request!
🔗 GitHub Issues


⭐ Support the Project

If you find this package useful, give it a star ⭐ on GitHub and share it with others! 🚀

Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.0.0 86 2/25/2025

- Initial release of EntityFrameworkCore.AuditInterceptor.