Handle CreatedDate,ModifiedDate,CreatedBy and ModifiedBy in Entity Framework

What is created date and Modified date and when we use this?
Keeping CreatedDate, ModifiedDate,  CreatedBy, and ModifiedBy or auditing columns in the database tables is part of good practices for database design. these columns help in many scenarios like select all the record by created date,Modified date.

If you have any transaction table like customer info, Order Details,EmployeeInfor then you can use CreatedDate and ModifiedDate.

If you have any tables which have user action like Order is placed by this user and modified by this user in this scenario you can use all four columns CreatedDate, ModifiedDate,  CreatedBy, and ModifiedBy
Let’s start with some coding part.

Public class Employee
{
  public int ID{get;set;}

  public String Name {get;set;}

  public String CreatedDate {get;set;}
  public String ModifiedDate{get;set;}
  public String CreatedBy{get;set;}
  public String ModifiedBy{get;set;}
}

While adding or modifying the employee class you have to take care of these two properties because we can not provide these properties in the user form as input.

Note: If you have one or two classes then you can manage to add additional code without any issue but if you have thousands of classes then obviously you will think of some generic code which can handle these two properties automatically while doing all add or edit operation.

As we discussed earlier auditing information is not required in all the tables.

public class BaseClass
{

  public String CreatedDate {get;set;}
  public String ModifiedDate{get;set;}
  public String CreatedBy{get;set;}
  public String ModifiedBy{get;set;}
}

There are two advantages of using BaseClass here

  • No need to write auditing columns properties in all the entity classes
  • It will be easy to differentiate between the Auditing entity vs normal entity class

If you are using entity framework then we can override the SaveChanges method inside the DbContext class and perform our auditing columns logic.

  public override int SaveChanges()
       {
    var entities = ChangeTracker.Entries().Where(x => x.Entity is BaseEntity && (x.State == EntityState.Added || x.State == EntityState.Modified));
           var currentUsername = HttpContext.Current != null && HttpContext.Current.User != null
                ? HttpContext.Current.User.Identity.Name
                : "Anonymous";
 foreach (var entity in entities)
        {
           if (entity.State == EntityState.Added)
              {
                    ((BaseEntity)entity.Entity).CreatedDate = DateTime.Now;
                    ((BaseEntity)entity.Entity).CreatedBy = currentUsername;
                }

                ((BaseEntity)entity.Entity).ModifiedDate = DateTime.Now;
                ((BaseEntity)entity.Entity).ModifiedBy = currentUsername;
            }
 return base.SaveChanges();
      }
Catch us on social media

Author Profile

Deependra Kushwah
Deependra Kushwah
Deependra is a Senior Developer with Microsoft technologies, currently working with Opteamix India business private solution. In My Free time, I write blogs and make technical youtube videos. Having the good understanding of Service-oriented architect, Designing microservices using domain driven design.

Deependra Kushwah

Deependra is a Senior Developer with Microsoft technologies, currently working with Opteamix India business private solution. In My Free time, I write blogs and make technical youtube videos. Having the good understanding of Service-oriented architect, Designing microservices using domain driven design.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

RSS
Facebook
Google+
https://betechnical.blog/2016/06/07/handle-createddatemodifieddatecreatedby-and-modifiedby-in-entity-framework">
Twitter
YouTube
Pinterest
Pinterest
LinkedIn
Instagram
%d bloggers like this: