开发者

Field change tracking in Entity Framework

开发者 https://www.devze.com 2023-04-01 11:40 出处:网络
Is there a way to find out what field has been changed on my object. Sample problem : when some开发者_如何学C date field is changed and the changes are being saved custom some custom logic should be

Is there a way to find out what field has been changed on my object.

Sample problem : when some开发者_如何学C date field is changed and the changes are being saved custom some custom logic should be applied to related objects.

I am trying to do it before context.SaveChanges(); but ObjectStateManager doesn't give me all the information I need.


Why to use ObjectStateManager for that? You have entities which have getter and setter so simply put that logic trigger in setter. You can also implement INotifyPropertyChanged on your entity and handle event when your required property changes outside of the entity.

Otherwise you must use ObjectStateManager get ObjectStateEntry for the entity and compare its CurrentValues and OriginalValues. But this is mostly wrong approach. Logic should be triggered by code in the entity.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号