实体框架 - 如何为任何财产募集OnChanging? [英] Entity Framework - how to raise OnChanging for any property?

查看:112
本文介绍了实体框架 - 如何为任何财产募集OnChanging?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在WPF / EF4.0 / MVVM应用程序中,我有一个视图编辑客户实体。在我的CustomerViewModel中设置一个属性bool IsCustomerInEditMode的最佳方法是对Entity的每个单个属性执行OnChanging / OnChanged部分方法?据我所知,没有OnEntityChanging方法...

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...

谢谢!

编辑:答案:EntityState

Answer: EntityState

推荐答案

编辑:
在你的帖子中回答你的问题(最好的方法来设置bool IsCustomerInEditMode) -

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

订阅entity.PropertyChanging事件,里面设置IsCustomerInEditMode == true;
订阅entity.PropertyChanged事件,里面设置IsCustomerInEditMode == false;

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

我认为InstanceOfYourCustomer.PropertyChanging和InstanceOfYourCustomer.PropertyChanged你正在看的事件对于。对于您的实体上的每个生成的属性,如果属性更改,则会触发事件(除非您使用部分类向实体添加其他属性,否则,您需要在这些属性的setter中添加对ReportPropertyChanging和ReportPropertyChanged的调用)。

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

我是使用EF4,并查看我的Model.Designer.cs文件...我所有的实体的属性设置器调用ReportPropertyChanging和ReportPropertyChanged ...它将触发您的实体上的PropertyChanging和PropertyChanged事件,并且args甚至会告诉您哪个特定的属性是引发已更改的事件。

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.

这篇关于实体框架 - 如何为任何财产募集OnChanging?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆