如何使由实体框架生成的类实现INotifyPropertyChanged?实体、框架、INotifyPropertyChanged

2023-09-05 03:54:03 作者:慢走不送

我使用MVVM模式在WPF中创建程序。

I am using MVVM pattern in WPF to create a program.

我平静地执行INPC在我的模型类....直到前几天,当我决定使用的EntityFramework。现在,我不知道我可以告诉的EntityFramework实施INPC上生成的实体类,每一次提高的属性更改事件属性的变化。

I was peacefully implementing INPC on my model classes .... until a few days ago when I decided to use EntityFramework. Now I dont know how I can tell the EntityFramework to implement INPC on the generated entity classes and raise the property changed event every time a property changes.

其他的问题,如this只是建议实施INPC在视图模型,但我真正想要的模型类来实现INPC减少工作量。

Other questions such as this simply suggest to implement INPC on ViewModel, but I really want the Model classes to implement INPC to reduce the amount of work.

我使用的模型第一种方法来生成我的模型。

I am using Model-first approach to generate my model.

推荐答案

近期发布的微软棱镜的团结容器的地址直奔其行为拦截功能这个问题。使用该功能可以使任何类的 BEHAVE 的,就像它从INotifyPropertyChanged的继承。

The recent release of Microsoft Prism's "Unity Container" addresses this question straight on with its 'Behaviour Interception' feature. Using the feature can make any class BEHAVE as though it inherits from INotifyPropertyChanged.

最引人注目的吸引力的特点是,你的EF类可以基本上感觉不到发生了什么,不要为了工作需要手工编码的修改。正如你所期望的,还有,你必须衡量你的需求的性能损失。

The most compelling attraction to the feature is that your EF classes can remain blissfully unaware of what's happening and do not need manual coding modifications in order to work. As you might expect, there's a performance hit which you have to measure against your requirements.

您可以在这里下载棱镜。 棱镜

You can download Prism here. Prism

有一个步行通过,你应该能够剪切和粘贴到您的code在这里。 拦截行为的您可以连接起来从这个演练在你的应用程序的启动阶段code,并统一将处理剩下的...

There's a walk-through that you SHOULD be able to cut-and-paste into your code here. Interception Behaviour You can wire up the code from this walkthrough in your app's start up phase, and Unity will handle the rest...

有一些code所有准备是一个很大的优势,因为它可能会很麻烦,如果你以前没有合作过型注射,所以我推荐了演练。

Having some code all ready is a great advantage because it can get hairy if you haven't worked with Type Injection before, so I recommend the walkthrough.