Introduction
In Entity Framework starting from version 4 and above we have the option to override the default “SaveChanges()” method and use some custom code or some business logic before committing the changes to the database. And this feature is very handy when we have entities with common properties that need to be handled automatically. We have encountered a few scenarios where we have entities with a modified date and modified by the user property that needs to be be updated correctly each time we deal with those entities. So rather than assigning a value to those properties every time throughout the application, what we can do is to have a single place with some logic written for the entities to populate the modified date and modified user name and than we can save the changes to the database. The scenario that I am talking about can be easily handled by overriding the “SaveChanges()” method of Entity Framework. Let's understand it with an example below.
Explanation
Let us say we have three entities named Employee, Department and Employment and they look something like:
- public class Department : EntityInformation
- {
- public int DepartmentID { get; set; }
- public string DepartmentName { get; set; }
- public string Location { get; set; }
- public virtual ICollection<Employment> Employments { get; set; }
- }
- public class Employee : EntityInformation
- {
- public int EmployeeID { get; set; }
- public string Name { get; set; }
- public int Age { get; set; }
- public virtual ICollection<Employment> Employments { get; set; }
- }
- public class Employment
- {
- public int EmploymentID { get; set; }
- public int EmployeeID { get; set; }
- public int DepartmentID { get; set; }
- public virtual Department Department { get; set; }
- public virtual Employee Employee { get; set; }
- }
- public class EntityInformation
- {
- public DateTime ModifiedDate { get; set; }
- public string ModifiedUserName { get; set; }
- }
As stated earlier, here we will override the default “SaveChanges()” method of Entity Framework. Since we are overriding “SaveChanges()” our context class will look as in the following.
- public class OrganizationContext : DbContext
- {
- public OrganizationContext()
- : base("OrganizationContext")
- {
- }
- public DbSet<Employee> Employees { get; set; }
- public DbSet<Department> Departments { get; set; }
- public DbSet<Employment> Employments { get; set; }
- protected override void OnModelCreating(DbModelBuilder modelBuilder)
- {
- modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
- }
- /// <summary>
- /// Overriding Save Changes
- /// </summary>
- /// <returns></returns>
- public override int SaveChanges()
- {
- var selectedEntityList = ChangeTracker.Entries()
- .Where(x => x.Entity is EntityInformation &&
- (x.State == EntityState.Added || x.State == EntityState.Modified));
- //Gt user Name from session or other authentication
- var userName = "MUKESH";
- foreach (var entity in selectedEntityList)
- {
- ((EntityInformation)entity.Entity).ModifiedDate = DateTime.Now;
- ((EntityInformation)entity.Entity).ModifiedUserName = userName;
- }
- return base.SaveChanges();
- }
- }
- var selectedEntityList = ChangeTracker.Entries()
- .Where(x => x.Entity is EntityInformation &&
- (x.State == EntityState.Added || x.State == EntityState.Modified));
- foreach (var entity in selectedEntityList)
- {
- ((EntityInformation)entity.Entity).ModifiedDate = DateTime.Now;
- ((EntityInformation)entity.Entity).ModifiedUserName = userName;
- }
- return base.SaveChanges();
So now whenever you call “SaveChanges()” with the entities of type “EntityInformation” the “ModifiedDate” and “ModifiedUserName” will be handled automatically and will be populated for that entity.
I hope this will help!

Heyzel TangPosted Dec 18, 2019, 5:25 AM
Why is it that it results to an error 'IEnumerable<DbEntityEntry>' does not contain a definition for 'Where' and no accessible extension method 'Where' accepting a first argument of type 'IEnumerable<DbEntityEntry>' could be found (are you missing a using directive or an assembly reference?) Thank you for your help
Carlos BaezPosted Jul 4, 2019, 10:31 AM
Good Job brother.
Bhavik PatelPosted May 6, 2016, 8:55 AM
nice
Tom MohanPosted Mar 30, 2015, 4:41 AM
nice one
Vithal WadjePosted Mar 29, 2015, 12:55 PM
nice
Rahul Kumar SaxenaPosted Mar 29, 2015, 12:18 AM
Good Show