Helping you solve those tough coding problems!

Updating specific fields using Entity Framework Updating specific fields using Entity Framework

Published on Mar 25, 2019 by Jamie Munro

Entity framework does a great job of detecting when a field changes on any object that it is tracking. These changes will be automatically saved when SaveChanges is executed. As I discussed in Why the Repository Pattern I like to create a Generic Class that is type T where T is a Model in my Entity Framework Context. This example will extend upon those examples.




Quick overview of the EntityState.Modified



In my Generic Repository class, I often create a function that looks as follows:


        public void UpdateAndSave(T entity)
        {
            using (var context = new MyContext())
            {
                context.Entry(entity).State = EntityState.Modified;
                context.SaveChanges();
            } 
        }


The following function takes a model of type T. Using the DbContext and the Entry function, the object's State is modified to EntityState.Modified. This tells Entity Framework to update the entire object in the database.

Perfect for 99% of the time.

Using the IsModified Property



On the rare occassion I want to ensure Entity framework only updates 1 or more specific fields of my object and not the entire object.

This can be accomplished by setting the IsModified property to true. Let's take a look at the function:


        public void UpdateFieldsSave(T entity, params Expression>[] includeProperties)
        {
            using (var context = new MyContext())
            {
                var dbEntry = context.Entry(entity);

				foreach (var includeProperty in includeProperties)
				{
					dbEntry.Property(includeProperty).IsModified = true;
				}
				
                context.SaveChanges();
            } 
        }


This function also takes a model of type T. The second parameter is actual an unlimited list of fields. This list will be looped setting the IsModified to true.

Once my DbContext is instantiated I fetch the DbEntry based on my model. I then loop the list of properties defined in my input parameter.

Inside the loop each property on my DbEntry each property notifies Entity framework that the property has been modified. When SaveChanges is called, only the properties I identified are updated in the database.

This function would be executed as follows:


var repository = new GenericRepository();

var book = repository.GetById(1);
book.Title = "My new title";

repository.UpdateFields(book, b => b.Title);


In this code I instantiate GenericRepository. This class should be created that contains the UpdateFields function.

Next the Book model is fetched using a GetById function. The Title property is changed and I call the UpdateFields function indicating that I only wish to update that field.

The repository examples are contained on GitHub: https://github.com/endyourif/RepoTest/.

Tags: Entity Framework | mvc

My Books
ASP.NET MVC 5 With Bootstrap and Knockout.js
Knockout.js Building Dynamic Client-Side Applications
20 Recipes for Programming MVC 3
20 Recipes for Programming PhoneGap
Rapid Application Development with CakePHP