Krutal Modi
Krutal Modi

Reputation: 487

How can I check which property of the entity is modified before SaveChanges?

I am using the EF with asp.net mvc5. I want the property which is modified while the update/edit entity. I can get it using the context.Entry(blog).Property("Name").IsModified = true; but for this I have to check each property for this entity. Is there any way I can get the direct property name and updated value?

Upvotes: 0

Views: 1374

Answers (1)

Justin Ricketts
Justin Ricketts

Reputation: 236

To get the property names that have changed:

var entry = context.Entry(myEntity);
var changedProperties = entry.CurrentValues.PropertyNames
    .Where(p => entry.Property(p).IsModified);

Upvotes: 2

Related Questions