Bium.Auditing.EntityFrameworkCore 1.0.0

There is a newer version of this package available.
See the version list below for details.
dotnet add package Bium.Auditing.EntityFrameworkCore --version 1.0.0
                    
NuGet\Install-Package Bium.Auditing.EntityFrameworkCore -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="Bium.Auditing.EntityFrameworkCore" Version="1.0.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Bium.Auditing.EntityFrameworkCore" Version="1.0.0" />
                    
Directory.Packages.props
<PackageReference Include="Bium.Auditing.EntityFrameworkCore" />
                    
Project file
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 Bium.Auditing.EntityFrameworkCore --version 1.0.0
                    
#r "nuget: Bium.Auditing.EntityFrameworkCore, 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.
#:package Bium.Auditing.EntityFrameworkCore@1.0.0
                    
#: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=Bium.Auditing.EntityFrameworkCore&version=1.0.0
                    
Install as a Cake Addin
#tool nuget:?package=Bium.Auditing.EntityFrameworkCore&version=1.0.0
                    
Install as a Cake Tool

Bium.Auditing.EntityFrameworkCore NuGet version NuGet Downloads

Bium.Auditing.EntityFrameworkCore is a lightweight library that provides automatic auditing capabilities for Entity Framework Core. It simplifies tracking creation, modification, and deletion information (including soft deletes) for your entities. This package is ideal for applications where audit trails are required for data changes.

This package is designed to be used with a SaveChangesInterceptor to automatically apply auditing logic.

Features

DbContext Auditing Extensions (DbContextExtensions)

  • ApplyAuditing(dbContext)
    Applies automatic auditing timestamps to all tracked entities without tracking the user.

  • ApplyAuditing<TPrimaryKey>(dbContext, performedBy) Applies auditing timestamps using the current UTC time and sets the user performing the operation.

    • The performedBy argument should be provided manually, retrieved from HttpContextAccessor, or obtained via a custom user service.
  • ApplyAuditing<TPrimaryKey, TDateTime>(dbContext, performedBy, performedAt) Applies auditing timestamps using a custom time (DateTime or DateTimeOffset) and sets the user performing the operation.

    • The performedBy argument should be provided manually, retrieved from HttpContextAccessor, or obtained via a custom user service.
    • The performedAt argument should be provided manually or via a custom datetime provider.

Getting Started

ApplyAuditing automatically applies audit information (creation, modification, deletion) to all entities implementing IAuditKind when SaveChanges or SaveChangesAsync is called. Using it with a SaveChangesInterceptor ensures audit trails are applied consistently without manually calling the methods in each repository or service.

Key Points

  • Automatically sets creation time and creator ID for new entities.
  • Automatically sets modification time and modifier ID for updated entities.
  • Supports soft deletes: automatically sets deletion time and deleter ID without physically removing records.
  • Easy integration with any DbContext.

Interceptor Examples

1. ApplyAuditing()

public class AuditingInterceptor : SaveChangesInterceptor
{
    public override InterceptionResult<int> SavingChanges(DbContextEventData eventData, InterceptionResult<int> result)
    {
        if (eventData.Context != null)
            eventData.Context.ApplyAuditing();

        return base.SavingChanges(eventData, result);
    }

    public override ValueTask<InterceptionResult<int>> SavingChangesAsync(
        DbContextEventData eventData,
        InterceptionResult<int> result,
        CancellationToken cancellationToken = default)
    {
        if (eventData.Context != null)
            eventData.Context.ApplyAuditing();

        return base.SavingChangesAsync(eventData, result, cancellationToken);
    }
}

2. ApplyAuditing<TPrimaryKey>(dbContext, performedBy)

public class AuditingWithUserInterceptor : SaveChangesInterceptor
{
    private readonly ICurrentUserService _currentUserService;

    public AuditingWithUserInterceptor(ICurrentUserService currentUserService)
    {
        _currentUserService = currentUserService;
    }

    public override InterceptionResult<int> SavingChanges(DbContextEventData eventData, InterceptionResult<int> result)
    {
        if (eventData.Context != null)
            eventData.Context.ApplyAuditing(_currentUserService.UserId);

        return base.SavingChanges(eventData, result);
    }

    public override ValueTask<InterceptionResult<int>> SavingChangesAsync(
        DbContextEventData eventData,
        InterceptionResult<int> result,
        CancellationToken cancellationToken = default)
    {
        if (eventData.Context != null)
            eventData.Context.ApplyAuditing(_currentUserService.UserId);

        return base.SavingChangesAsync(eventData, result, cancellationToken);
    }
}

3. ApplyAuditing<TPrimaryKey, TDateTime>(dbContext, performedBy, performedAt)

public class AuditingWithUserAndTimeInterceptor : SaveChangesInterceptor
{
    private readonly ICurrentUserService _currentUserService;
    private readonly IDateTimeProvider _dateTimeProvider;

    public AuditingWithUserAndTimeInterceptor(ICurrentUserService currentUserService, IDateTimeProvider dateTimeProvider)
    {
        _currentUserService = currentUserService;
        _dateTimeProvider = dateTimeProvider;
    }

    public override InterceptionResult<int> SavingChanges(DbContextEventData eventData, InterceptionResult<int> result)
    {
        if (eventData.Context != null)
            eventData.Context.ApplyAuditing(_currentUserService.UserId, _dateTimeProvider.UtcNow);

        return base.SavingChanges(eventData, result);
    }

    public override ValueTask<InterceptionResult<int>> SavingChangesAsync(
        DbContextEventData eventData,
        InterceptionResult<int> result,
        CancellationToken cancellationToken = default)
    {
        if (eventData.Context != null)
            eventData.Context.ApplyAuditing(_currentUserService.UserId, _dateTimeProvider.UtcNow);

        return base.SavingChangesAsync(eventData, result, cancellationToken);
    }
}

Interceptor Usage

  1. No user trackingAuditingInterceptor
  2. Track userAuditingWithUserInterceptor
  3. Track user + custom timestampAuditingWithUserAndTimeInterceptor

Make sure to register the interceptor as a scoped service and add it to your DbContext configuration.

Contributing

Contributions are welcome! Please follow the standard GitHub workflow:

  1. Fork the repository
  2. Create a new branch (feature/your-feature)
  3. Commit your changes
  4. Open a pull request

License

Bium.Auditing.EntityFrameworkCore is licensed under the MIT License.

Product Compatible and additional computed target framework versions.
.NET net5.0 is compatible.  net5.0-windows was computed.  net6.0 is compatible.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 is compatible.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  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 is compatible.  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.  net10.0 was computed.  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.

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.2 0 9/22/2025
1.0.1 239 9/18/2025
1.0.0 242 9/17/2025

- Initial Release