Lajith
Lajith

Reputation: 1867

Audit log in asp.net core and dapper

I need to implement audit log using asp.net core and dapper.net.. I found solutions in Entity Framework 7 audit log

The solution provided here are in the EF Core (Shadow Properties) but I want to implement same using dapper.net. My Interface looks like in ASP.NET Core

public interface IAuditableEntity 
{
    int? CreatedById { get; set; }

    DateTime Created { get; set; }

    int? ModifiedById { get; set; }

    DateTime Modified { get; set; }
} 

public override int SaveChanges()
{
    int? userId = null;
    if (System.Web.HttpContext.Current != null)
        userId = (from user in Users.Where(u => u.UserName == System.Web.HttpContext.Current.User.Identity.Name) select user.Id).SingleOrDefault();

    var modifiedBidEntries = ChangeTracker.Entries<User>()
        .Where(e => e.State == EntityState.Added || e.State == EntityState.Modified);

    foreach (EntityEntry<User> entry in modifiedBidEntries)
    {
        entry.Property("Modified").CurrentValue = DateTime.UtcNow;
        entry.Property("ModifiedById").CurrentValue = userId;

        if (entry.State == EntityState.Added)
        {
            entry.Property("Created").CurrentValue = DateTime.UtcNow;
            entry.Property("CreatedById").CurrentValue = userId;
        }
    }

    return base.SaveChanges();
}

Please any one help me to get the solutions in dapper.net

Upvotes: 0

Views: 3074

Answers (1)

Marc Gravell
Marc Gravell

Reputation: 1063338

No, dapper has no support for this scenario. That said, you can of course do anything you like to the objects you pass into dapper.

Upvotes: 2

Related Questions