Last Updated: February 25, 2016
·
428
· tellez12

How to choose what fields not to Update in EF

When updating an Entity in EF that have a CreatedDate or any other property that you don't want to update, you have two options, either store and keep the original value or you can tell EF that some properties haven't been modified, even if they have been.

var myEntry = context.Entry(entity);
myEntry .State = EntityState.Modified;
foreach (var name in excludedProperties)
{
    entry.Property(name).IsModified = false;
}

Where excludedProperties is a list of strings containing all the properties to exclude, and entity the entity to be modified.