user1841243
user1841243

Reputation: 1713

How to detect changes in 1 entity framework object

I have a list of entities and when some entities change I just perform the following:

DBContext.SaveChanges();

and all modifications are saved.

but now I have to perform some business logic when certain entities are changed (and only for the ones that are changed) and need to find those changed entities. But I can't seem to be able to do this.

I've come across this post: Entity Framework 5 - Why is Entity State "Modified" after PropertyValue is set back to Original

where someone checks the .EntityState property ....

But my entity does not have this property.

As a last resort I could use INotifyPropertyChanged on the level of my model, but would be nice if EF5 would let me know the state of that entity.

Upvotes: 1

Views: 3837

Answers (1)

DrinkBird
DrinkBird

Reputation: 834

To check the state of an entity use the following:

DbEntityEntry entry = Context.Entry(entity); //where Context is DbContext or derived

// entry.State is available here

Upvotes: 5

Related Questions