locked
Audit Trail in Entity Framework 6.1.3? RRS feed

  • Question

  • User1224379429 posted

    Hi 

    I am using generic repository pattern for CRUD operations. I want to audit trail when User modify record. I want to log modified property name, current value and new value. How to log this using EF 6.1.3?

      public class DataRepository<TEntity> : IRepository<TEntity> where TEntity : class
        {
        public bool Update(TEntity entity)
            {
                try
                {
                    using (rateEntities entities = new rateEntities())
                    {
                        entities.Entry(entity).State = System.Data.Entity.EntityState.Modified;
                        entities.SaveChanges();
                        return true;
                    }
                }
                catch (Exception exception)
                {
                    throw exception;
                }
            }
    }

    Thanks

    Tuesday, January 10, 2017 10:31 AM

All replies

  • User-1838255255 posted

    Hi Athar Ali Khan,

    According to your description, as far as I know, your wanted technology is tracker-enabled-dbcontext.

    It can track the changes in database, also can record new value, record old value, It will also audit the time of change and user who changed/added/deleted the record. I found a tutorial about how to operate it, please refer to this link:

    https://github.com/bilal-fazlani/tracker-enabled-dbcontext/wiki

    Best Regards

    Eric Du

    Wednesday, January 11, 2017 8:54 AM
  • User1224379429 posted

    Hi Eric,

    Thanks for your reply. I am not using DBContext but Entities with generic repository pattern.  I found the below link very helpful but I want implement the audit trail on entity framework  using generic repository pattern.

    http://www.c-sharpcorner.com/UploadFile/ff2f08/working-with-change-tracking-proxy-in-entity-framework-6-0/

    Thursday, January 12, 2017 9:04 AM
  • User1397563414 posted

    Hi Athar Ali Khan,

     I found the below link very helpful but I want implement the audit trail on entity framework  using generic repository pattern.

    If you want to use generic repository pattern, you can try to create a class(RepositoryBase.cs), and the DataRepository class implement the RepositoryBase class.

    Implement the update and some other generic method in the RepositoryBase class, and add the special method in the DataRepository class.

    You can get help from (http://codereview.stackexchange.com/questions/19037/entity-framework-generic-repository-pattern).

    Best Regards,
    JiyaoLee

    Wednesday, February 8, 2017 3:59 AM