Rejecting Changes to a Single Property in Entity Framework 6.0

Introduction

 
I previously wrote an article about how to discard changes of DbContext / ObjectContext in Entity Framework 6.0 without disposing of DbContext / ObjectContext. But this will discard all changes at the context level. Now I want to only discard some properties. Entity Framework is now able to reject changes to an individual property.
 
The Entity Framework maintains two values for each property of the tracked entity, current value, and original value. For both names suggest themself, the current value is the current value of the property and the original value is the value of the property when the entity was queried from the data source or attached to the context.
 
The property is marked as modified if the DetectChanges method determines that the property's current value is different from the original value (the value it had when the entity was queried or the entity was attached to the context). When we set the state of an entity to modify, then all the properties are marked as modified.
 
Properties are marked as modified will be important because these values will be sent to the database when the SaveChanges method of the context is called.
 
Checking whether a property is marked as modified
 
DbPropertyEntry has a property called "IsModified", it gets or sets a value indicating whether the value of this property has been modified from its original value.
  1. bool isCodeChanged = context.Entry(data).Property(p => p.Code).IsModified;  
We can also set this property to true or false. The following code forces the property value sent to the database even if it has not changed from its original value.
  1. context.Entry(data).Property(p => p.Code).IsModified = true;  
Example
 
Suppose I have the following code that has modified the code property of the “Employee” entity.
  1. static void Main(string[] args)  
  2. {  
  3.    using (SQLLogging.Model.Model context = new SQLLogging.Model.Model())  
  4.    {  
  5.       var data = context.Employees.Where(p => p.Id == 1).FirstOrDefault();  
  6.       if (data != null)  
  7.       {  
  8.          data.Code = "ABB";  
  9.       }  
  10.       context.SaveChanges();  
  11.    }  
  12.    Console.ReadLine();  
  13. }  
To trace the query generated by Entity Framework, I must enable SQL logging.
  1. public partial class Model : DbContext  
  2. {  
  3.    public Model() : base("name=EntityModel")  
  4.    {  
  5.    }  
  6.    ….  
  7.    ….  
  8.    ….  
  9.    protected override void OnModelCreating(DbModelBuilder modelBuilder)  
  10.    {  
  11.       Database.Log = Console.WriteLine;   
  12.    }  
  13. }  
Now run the code and check the output. Here the Entity Framework generates the update query.
 
Output
 
Output
 
Now in the following code snippet, I have forcefully reverted the change to the code property of the employee entity and checked the output.
  1. static void Main(string[] args)  
  2. {  
  3.     using (SQLLogging.Model.Model context = new SQLLogging.Model.Model())  
  4.     {  
  5.         var data = context.Employees.Where(p => p.Id == 1).FirstOrDefault();  
  6.         if (data != null)  
  7.         {  
  8.             data.Code = "ABC";  
  9.             //check whether code property is modify of not?  
  10.             if (context.Entry(data).Property(p => p.Code).IsModified)  
  11.             {  
  12.                 //Undoing property level change  
  13.                 context.Entry(data).Property(p => p.Code).IsModified = false;  
  14.             }  
  15.         }  
  16.         context.SaveChanges();  
  17.     }  
  18.     Console.ReadLine();  
  19. }  
Output
 
command prompt
 
When we tell Entity Framework that a property is no longer modified the context will do the following two things:
  1. Context will reset the current value to the original value. If the original value is not known then this will have no observable effect.
  2. Context will reset the modified flag.
Because the modified flag is reset this means that when SaveChanges is called the value of the property will not be sent to the database. Obviously, this is a very small feature of Entity Framework (with .Net Framework 4.5) but it is useful.