Entity Framework - how to raise OnChanging for any property?

我是研究僧i 提交于 2019-12-12 09:22:09

问题


In a WPF / EF4.0 / MVVM app I have a View that edits a Customer entity. What is the best way to set a property "bool IsCustomerInEditMode" in my CustomerViewModel short of acting upon OnChanging/OnChanged partial methods for every single individual property of the Entity? As far as I know there's no OnEntityChanging method...

Thanks!

Edit: Answer: EntityState


回答1:


Edit: To answer your question in your post (best way to set bool IsCustomerInEditMode) -

Subscribe to the entity.PropertyChanging event, inside it set IsCustomerInEditMode == true; Subscribe to the entity.PropertyChanged event, inside it set IsCustomerInEditMode == false;

I think InstanceOfYourCustomer.PropertyChanging and InstanceOfYourCustomer.PropertyChanged the events you're looking for. For every generated property on your entity, the event fires if a property changes (unless you use partial classes to add additional properties to your entity, in which case, you'll need to add calls to ReportPropertyChanging and ReportPropertyChanged in the setters of those properties).

http://msdn.microsoft.com/en-us/library/system.data.objects.dataclasses.structuralobject.propertychanged.aspx

http://msdn.microsoft.com/en-us/library/system.data.objects.dataclasses.structuralobject.propertychanging.aspx

I'm using EF4, and looking at my Model.Designer.cs file... all of my entities' properties' setters call ReportPropertyChanging and ReportPropertyChanged... which will fire the PropertyChanging and PropertyChanged events on your entity, and the args will even tell you which specific property it was that raised the changed event.



来源:https://stackoverflow.com/questions/3460714/entity-framework-how-to-raise-onchanging-for-any-property

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!