AuditaX.EntityFramework
1.0.4
Requires NuGet 6.0 or higher.
dotnet add package AuditaX.EntityFramework --version 1.0.4
NuGet\Install-Package AuditaX.EntityFramework -Version 1.0.4
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="AuditaX.EntityFramework" Version="1.0.4" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="AuditaX.EntityFramework" Version="1.0.4" />
<PackageReference Include="AuditaX.EntityFramework" />
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add AuditaX.EntityFramework --version 1.0.4
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: AuditaX.EntityFramework, 1.0.4"
#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.
#:package AuditaX.EntityFramework@1.0.4
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=AuditaX.EntityFramework&version=1.0.4
#tool nuget:?package=AuditaX.EntityFramework&version=1.0.4
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
AuditaX.EntityFramework
Entity Framework Core support for AuditaX audit logging library.
Installation
dotnet add package AuditaX
dotnet add package AuditaX.EntityFramework
dotnet add package AuditaX.SqlServer # or AuditaX.PostgreSql
Features
- Automatic audit logging via EF Core interceptors
- Captures Added, Modified, and Deleted entity states
- Automatic change detection using EF Core's change tracker
- Transaction-safe: audit logs saved in same transaction as entity changes
- Zero code changes in your services/repositories
Configuration
appsettings.json
{
"AuditaX": {
"TableName": "AuditLog",
"Schema": "dbo",
"LogFormat": "Json",
"AutoCreateTable": true,
"EnableLogging": true,
"Entities": {
"Product": {
"SourceName": "Product",
"Key": "ProductId",
"Properties": [ "Name", "Price", "Stock" ]
}
}
}
}
Service Registration
// Register DbContext WITH AuditaX interceptors
services.AddDbContext<AppDbContext>((sp, options) =>
{
options.UseSqlServer(connectionString);
options.AddAuditaXInterceptor(sp); // Important!
});
// Configure AuditaX
services.AddAuditaX(configuration)
.UseEntityFramework<AppDbContext>()
.UseSqlServer() // or .UsePostgreSql()
.ValidateOnStartup();
Usage
With Entity Framework Core, auditing is automatic. No changes needed in your code:
public class ProductService(AppDbContext context)
{
public async Task<Product> CreateAsync(Product product)
{
context.Products.Add(product);
await context.SaveChangesAsync(); // Audit log created automatically!
return product;
}
public async Task UpdateAsync(int id, string name, decimal price)
{
var product = await context.Products.FindAsync(id);
product!.Name = name;
product.Price = price;
await context.SaveChangesAsync(); // Audit log created automatically!
}
public async Task DeleteAsync(int id)
{
var product = await context.Products.FindAsync(id);
context.Products.Remove(product!);
await context.SaveChangesAsync(); // Audit log created automatically!
}
}
How It Works
- Interceptor Registration:
AddAuditaXInterceptor()registers aSaveChangesInterceptor - Change Detection: When
SaveChangesAsync()is called, the interceptor:- Enumerates all tracked entities
- Filters for Added, Modified, and Deleted states
- Checks if entity type is configured for auditing
- Change Comparison: For Modified entities:
- Gets original values from
OriginalValues - Compares with current values
- Creates
FieldChangerecords for differences
- Gets original values from
- Audit Logging: Creates audit log entries via
IAuditService - Transaction Safety: All operations occur within the same transaction
DbContext Requirements
Your DbContext must be registered with the AuditaX interceptors:
services.AddDbContext<AppDbContext>((sp, options) =>
{
options.UseYourDatabase(connectionString);
options.AddAuditaXInterceptor(sp); // Required for automatic auditing
});
Comparison with Dapper
| Feature | Entity Framework | Dapper |
|---|---|---|
| Change Tracking | Automatic via interceptors | Manual via IAuditUnitOfWork |
| Audit Timing | On SaveChangesAsync() |
Explicit calls in repository |
| Control | Automatic for all changes | Full control over when to audit |
| Setup | Add interceptors to DbContext | Inject IAuditUnitOfWork |
Documentation
For complete documentation, see the main AuditaX repository.
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net10.0 is compatible. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
-
net10.0
- AuditaX (>= 1.0.4)
- Microsoft.EntityFrameworkCore.Relational (>= 10.0.1)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
v1.0.4 - Fix: EF Core audit log now handles re-creation of previously deleted entities.