Home » Ado.Net » Entity Framework » How can I run an update on specific properties for detached objects?

How can I run an update on specific properties for detached objects?

In some scenario you will want to update a specific property but not others. This can be for security reason or for performance reason. In either case, the goal is to update only specific property of your entity. In Entity Framework, this can be done by specifying to the database context (dbcontext) the property status. Each property does have a IsModified property that can be set to true or false.

context.Entry(yourObject).Property(u => u.YourProperty).IsModified = true;

Of course, if your object is from view, this one is not known by Entity Framework’s database context and will require to attach it first.

using (var entities = new MyDbContext())
{
    var entity = new YourEntity {Id = id, Name="Test"};
    entities.YourEntities.Attach(entity);
    entities.Entry(entity).Property(e => e.Name).IsModified = true;
    entities.SaveChanges();
}

This will save YourEntity entity but only change the Name property. This is quite useful in scenario where you do not want the user to hack a web form to add properties that you doesn’t want to be read by Entity Framework.

By specifying which property that is modified, you tell Entity Framework to generate the SQL with only the “Set” for them. For example:

UPDATE YourEntity
SET Name = "Test"
WHERE Id = 123;

Finally, you may want to close the automatic validation if you are using the validation mechanism. This can be done by setting the ValidateOnSaveEnabled to false.

entities.Configuration.ValidateOnSaveEnabled = false;

The reason is that since not all the object is loaded, you may have some validation on field that should be required that isn’t at this time. To temporary disable the validation, use ValidateOnSaveEnabled. Of course, set it back to true once the save changes is done. You can find an example on my blog concerning the ValidateOnSaveEnabled.

If you like my article, think to buy my annual book, professionally edited by a proofreader. directly from me or on Amazon. I also wrote a TypeScript book called Holistic TypeScript

Leave a Reply

Your email address will not be published. Required fields are marked *

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