A simple library that adds audit capabilities to the Entity Framework Core.
Report Bug
·
Request Feature
With this library, you can automatically have an audit mechanism for labeled model classes in EF Core.
To get start with EFCore.Audit you can clone repository or add as a reference to your project from nuget.
Install-Package EFCore.Audit -Version 1.0.0
dotnet add package EFCore.Audit --version 1.0.0
<PackageReference Include="EFCore.Audit" Version="1.0.0" />
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 fromAuditDbContextBase
.
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 andNotAuditable
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; }
}
See the open issues for a list of proposed features (and known issues).
Distributed under the MIT License. See LICENSE
for more information.
Doğa Barış Çakmak - dogabaris.cakmak@gmail.com
Project Link: https://github.com/dogabariscakmak/EFCore.Audit
Created my free logo at LogoMakr.com