Skip to content

A simple library that adds audit capabilities to the Entity Framework Core.

License

Notifications You must be signed in to change notification settings

dogabariscakmak/EFCore.Audit

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

EFCore.Audit

Issues MIT License


Logo

EFCore.Audit

A simple library that adds audit capabilities to the Entity Framework Core.
Report Bug · Request Feature

Table of Contents

About The Project

EFCore.Audit

With this library, you can automatically have an audit mechanism for labeled model classes in EF Core.

Getting Started

To get start with EFCore.Audit you can clone repository or add as a reference to your project from nuget.

Package Manager

Install-Package EFCore.Audit -Version 1.0.0

.NET CLI

dotnet add package EFCore.Audit --version 1.0.0

PackageReference

<PackageReference Include="EFCore.Audit" Version="1.0.0" />

Usage

EFCore.Audit adds audit data by overriding and inserting audit logic. The library has its own audit entities to store audit data. To use EFCore.Audit, as a developer you need to do a couple of things.

  • Since EFCore.Audit override SaveChanges(), you need to inherit your DBContext class from AuditDbContextBase.
public class PersonDbContext :AuditDbContextBase<PersonDbContext>
{
    ...
}
  • You need also provide a class which implements IAuditUserProvider to the base class. This class provides user who did the operation.
public class PersonDbContext :AuditDbContextBase<PersonDbContext>
{
    public PersonDbContex(DbContextOptions<PersonDbContext> options,IAuditUserProvider auditUserProvider) : bas(options, auditUserProvider) { }
}
public class UserProvider : IAuditUserProvider
{
    private readonly IHttpContextAccessor_httpContextAccessor;
    public UserProvider(IHttpContextAccessorhttpContextAccessor)
    {
        _httpContextAccessor = httpContextAccessor;
    }
    public string GetUser()
    {
        return _httpContextAccessor.HttpContext.UserIdentity.Name;
    }
}
  • Also base class OnModelCreating() method should be called.
public class PersonDbContext :AuditDbContextBase<PersonDbContext>
 {
    ...

     protected override void OnModelCreating(ModelBuilder modelBuilder)
     {
         ...
         base.OnModelCreating(modelBuilder);
     }
 }
  • The last thing is that labeling auditable classes and excluding desired properties of auditable classes. To label which classes will be auditable and which attributes will be excluded, you need to use Auditable attribute for classes and NotAuditable attribute for properties.
[Auditable]
public class PersonEntity
{
    public Guid Id { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public GenderEnum Gender { get; set; }
    [NotAuditable]
    public string DummyString { get; set; }
}

Roadmap

See the open issues for a list of proposed features (and known issues).

License

Distributed under the MIT License. See LICENSE for more information.

Contact

Doğa Barış Çakmak - dogabaris.cakmak@gmail.com

Project Link: https://github.com/dogabariscakmak/EFCore.Audit

Created my free logo at LogoMakr.com

About

A simple library that adds audit capabilities to the Entity Framework Core.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published